In this section, you will explore how to work with launchers and choosers from within your application. You will use PhoneNumberChooserTask and SmsComposeTask
to create an application that selects a contact from the shared list of
contacts on the phone and then composes a text message to that
contact's phone.
1. Creating the User Interface
The user interface for this
sample consists of a single button; when the user clicks this button, a
list of contacts will be displayed, allowing the user to pick one.
Launch
Visual Studio 2010 Express, and select the Windows Phone Application
template. Change the project name to "Tasks," select OK, and Visual
Studio will set up a new project.
Open MainPage.xaml in design mode, and add a button to the page. Change the button's caption to be "Send SMS."
2. Coding Application Logic
When the user clicks the
"Send SMS" button of your application, she should be presented with a
list of contacts available on your device. Conveniently for us, even on
the emulator, Microsoft has included a sample list of contacts for us to
test an application with.
Open MainPage.xaml.cs (right-click MainPage.xaml and select View Code). At the top of the page, add the following using statement:
using Microsoft.Phone.Tasks;
Declare the following module-level variable for the PhoneNumberChooserTask chooser (insert it right above the MainPage() constructor):
private PhoneNumberChooserTask _choosePhoneNumberTask;
Instantiate a new PhoneNumberChooserTask object within the MainPage()
constructor, and associate a method to invoke when the user selects a
contact from the list. The method to call upon the chooser's return will
accept the phone number selected as one of the parameters. Use the
following two lines of code to accomplish that:
_choosePhoneNumberTask = new PhoneNumberChooserTask();
_choosePhoneNumberTask.Completed += new
EventHandler<PhoneNumberResult>(ChoosePhoneNumberTaskCompleted);
Code the ChoosePhoneNumberTaskCompleted method that will be invoked upon selection of a contact from the list. Note the use of an SmsComposeTask launcher to create a new text message to the person you have selected from the contact list:
private void ChoosePhoneNumberTaskCompleted(object sender, PhoneNumberResult e)
{
new SmsComposeTask() { Body = "SMS using Windows Phone 7 Chooser", To =
e.PhoneNumber }.Show();
}
Finally, add code to react to the button-click event by opening the PhoneNumberChooser launcher. The easiest way to accomplish this is to double-click the button with MainPage.xaml open in design view and make the button-click event look like the following:
private void button1_Click(object sender, RoutedEventArgs e)
{
_choosePhoneNumberTask.Show();
}
Press
F5 to run your application. Click the Send SMS button, and select
Andrew R. (Andy) Hill from the list of contacts that comes up on the
emulator. Immediately after selecting Andy, you should see a screen
similar to Figure 1, where the SMS message has been composed and is ready to be sent to Andrew.
To summarize what you have
learned so far, your application integrates with the Windows Phone OS
via a set of API methods referred to as launchers and choosers. Working
with launchers and choosers is fairly straightforward, as illustrated by
the foregoing example.
One major limitation
of mobile platforms is their inherently short battery life. This
limitation causes OS designers and phone manufacturers to come up with
various techniques to balance short battery life with positive user
experience. One such technique is to allow only one application to run
on the phone at any given time. You may be wondering then, what happens
when your application yields execution to the built-in Windows Phone OS
application invoked with the launcher? That brings us to the important
Windows Phone concept of application tombstoning, a subject you'll explore along with the Windows Phoneapplication life cycle in the next section.