Tasks: Launchers and Choosers–Windows Phone From Scratch #22

To provide your Windows Phone application access to the operating system (and with CHOOSER it, to the native applications such as SMS, the Contact List, the Camera, and, oh yes, making a call) Windows Phone 7 has a set of predefined Tasks.

Tasks can be divided into two types: Launchers, which launch an application but do not return a value (e.g., sending a SMS message) and Choosers, which launch an application and do return a value (e.g., fetching a phone number).

A key concept to keep in mind is that when you start a Task your application is tombstoned.  That is, it is likely to be terminated and the user may never return to your application (especially if you use a launcher, but even if you use a Chooser – they can always turn the phone off!)

In the case of a Chooser, when you return from the Task your call back method will be called, but only if you set it up as a private member variable and not as a local variable within a method.  Here’s how to do it…

Declare the task as a private member variable.

I started by creating a new Windows Phone Application and added a button that I named SMS to the Main Page.  I then declared the following at the top of the class, in the code behind,

public partial class MainPage : PhoneApplicationPage
{
   private PhoneNumberChooserTask _choosePhoneNumber;

The next step is to initialize the PhoneNumberChooserTask in the constructor for the page,

 public MainPage()
 {
    InitializeComponent();
    _choosePhoneNumber = new PhoneNumberChooserTask();

Now we’ll set up an event handler for the button to invoke this Task. You invoke a task by calling its Show method.

void SMS_Click( object sender, RoutedEventArgs e )
{
   _choosePhoneNumber.Show();
}

As noted above the task runs asynchronously – in fact it runs even while your application is being tombstoned.  You therefore need a callback method.  Here’s the complete code-behind file. Note the set up for the button’s even handler and for the Tasks’s call-back in the constructor.  In our case the callback will just open a message  box with the phone number chosen from the sample contact data supplied with the emulator,

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace SMS
{
   public partial class MainPage : PhoneApplicationPage
   {
      private PhoneNumberChooserTask _choosePhoneNumber;
      private SmsComposeTask _smsCompose;
      // Constructor
      public MainPage()
      {
         InitializeComponent();
         _choosePhoneNumber =
            new PhoneNumberChooserTask();
         _choosePhoneNumber.Completed +=
            new EventHandler<PhoneNumberResult>(
               _choosePhoneNumber_Completed );
         SMS.Click +=
            new RoutedEventHandler( SMS_Click );
      }

      void SMS_Click(
         object sender,
         RoutedEventArgs e )
      {
         _choosePhoneNumber.Show();
      }

      void _choosePhoneNumber_Completed(
         object sender,
         PhoneNumberResult e )
      {
         System.Windows.MessageBox.Show(
            "You picked the phone number " +
            e.PhoneNumber );
      }
   }
}

In the next mini-tutorial I’ll demonstrate how to take that phone number and use it to generate an SMS message (which is far easier than it sounds).

Unknown's avatar

About Jesse Liberty

** Note ** Jesse is currently looking for a new position. You can learn more about him at https://jesseliberty.bio Thank you. Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 13 year Microsoft MVP.
This entry was posted in Patterns & Skills and tagged , . Bookmark the permalink.

4 Responses to Tasks: Launchers and Choosers–Windows Phone From Scratch #22

  1. Abdul Haleem's avatar Abdul Haleem says:

    Iam getting an error on line number 21: “Error 1 The type or namespace name ‘Click’ does not exist in the namespace ‘SMS’ (are you missing an assembly reference?) ” can anyone tell me which assembly reference to add for this…???

  2. nikhil's avatar nikhil says:

    Hi,
    I am developing an app which sends multiple sms to same person simultaneously. Can you please help tell how to go about writing code to send multiple sms to same person.

  3. @David White: I know its a bit late, I am only discovering this site now. But if you still didnt get the answer to your question, a quick look up of the documentation on MSDN brings you to this http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks.phonenumberresult.displayname(v=VS.92).aspx

    So you can get the Contacts name by simple doing, e.DisplayName.

  4. David White's avatar David White says:

    Jesse, is there any way for the PhoneNumberChooserTask to return the Contact’s name, too? In my app, I’d like the user to choose a contact, and then I want access to the name and mobile phone number to re-use. Similar to assembling a list of next-of-kin.
    It seems pointless to choose a contact, and then get the user to type out the selected name again!

Comments are closed.