To provide your Windows Phone application access to the operating system (and with 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).
4 Responses to Tasks: Launchers and Choosers–Windows Phone From Scratch #22