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).

About Jesse Liberty

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 will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was 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 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

Comments are closed.