Click here to Skip to main content
15,860,972 members
Articles / Mobile Apps / Windows Phone 7

Windows Phone 7 Launchers & Choosers: The Absolute Guide

Rate me:
Please Sign up or sign in to vote.
4.83/5 (5 votes)
13 Dec 2010CPOL2 min read 28.3K   13   3
Windows Phone 7 launchers and choosers

Many people ask me why Windows Phone 7 does not allow access to native functionality in order to accomplish common tasks, such as sending SMS and e-mails, via our own applications. Well, you may not have direct access, but the Windows Phone 7 API offers indirect access to almost every common phone feature in the form of Launchers and Choosers.

If you're in a hurry, just download a complete demo which illustrates the use of every Launcher and Chooser. Read on to understand how things work... ;-)

Image 1

Let's begin with the definitions: Launchers start a native application and return no data to the calling application (such as opening a web browser). Choosers, on the other hand, start a native application and return some data back to the calling application (such as opening the contact list and selecting a particular phone number). We'll now see how Launchers and Choosers behave separately, as well as how they can be combined to work together!

Both Launchers and Choosers are found under Microsoft.Phone.Tasks namespace, so do not forget to include it in your application.

Using Launchers

Using a Launcher is pretty straightforward. Simply declare a phone-Task object, assign values to its properties and then call Show() method. Let's see specific examples:

PhoneCallTask (Starts a phone call)

C#
PhoneCallTask task = new PhoneCallTask();   
task.PhoneNumber = "1234567890";   
task.DisplayName = "Vangos Pterneas";   
task.Show();

SmsComposeTask (Creates a new SMS message)

C#
SmsComposeTask task = new SmsComposeTask();   
task.To = "1234567890";   
task.Body = "This is a sample SMS message.";   
task.Show();

EmailComposeTask (Creates a new e-mail message)

C#
EmailComposeTask task = new EmailComposeTask();   
task.To = "test@test.com";   
task.Cc = "test2@test.com";   
task.Subject = "Testing...";   
task.Body = "This is a sample e-mail message.";   
task.Show();

WebBrowserTask (Opens the web browser)

C#
WebBrowserTask task = new WebBrowserTask();   
task.URL = "http://vangos.eu";   
task.Show();

SearchTask (Launches search)

C#
SearchTask task = new SearchTask();   
task.SearchQuery = "Vangos Pterneas";   
task.Show();

MediaPlayerLauncher (Launches Media Player)

C#
MediaPlayerLauncher task = new MediaPlayerLauncher();   
task.Media = new Uri(
    "http://ecn.channel9.msdn.com/o9/ch9/4807/574807/ISWPE05SLToolKitForWP_ch9.wmv");   
task.Show();

Using Choosers

Using a Chooser is a little more complex. Choosers return a value, so, most of the times, we need to get this value through the Chooser's Completed event handler. Let's see how this can be done for each Chooser:

CameraCaptureTask (Takes a picture and returns it as a bitmap image)

C#
CameraCaptureTask task = new CameraCaptureTask();   
task.Completed += (s, evt) =>   
{   
   if (evt.Error == null && evt.TaskResult == TaskResult.OK)   
   {   
      BitmapImage bmpImage = new BitmapImage();   
      bmpImage.SetSource(evt.ChosenPhoto);   
      image.Source = bmpImage;   
   }   
};   
task.Show();

PhotoChooserTask (Let us select a photo)

C#
PhotoChooserTask task = new PhotoChooserTask();   
task.Completed += (s, evt) =>   
{   
   if (evt.Error == null && evt.TaskResult == TaskResult.OK)   
   {   
      BitmapImage bmpImage = new BitmapImage();   
      bmpImage.SetSource(evt.ChosenPhoto);   
      image.Source = bmpImage;   
   }   
};   
task.Show();

PhoneNumberChooserTask (Retrieves a phone number)

C#
PhoneNumberChooserTask task = new PhoneNumberChooserTask();   
task.Completed += (s, evt) =>   
{   
   if (evt.Error == null && evt.TaskResult == TaskResult.OK)   
   {   
      MessageBox.Show(evt.PhoneNumber + " phone number selected!");   
   }   
};   
task.Show();

EmailAddressChooserTask (Retrieves an e-mail address from our contact list)

C#
EmailAddressChooserTask task = new EmailAddressChooserTask();   
task.Completed += (s, evt) =>   
{   
   if (evt.Error == null && evt.TaskResult == TaskResult.OK)   
   {   
      MessageBox.Show(evt.Email + " e-mail address selected!");   
   }   
};   
task.Show();

SavePhoneNumberTask (Saves a specified phone number to a contact)

C#
SavePhoneNumberTask task = new SavePhoneNumberTask();   
task.PhoneNumber = "1234567890";   
task.Show();

SaveEmailAddressTask (Saves a specified e-mail address to a contact)

C#
SaveEmailAddressTask task = new SaveEmailAddressTask();   
task.Email = "test@test.com";   
task.Show();

Combining Launchers and Choosers!

What happens when you need more advanced functionality? For example, suppose you may want to select a phone number (PhoneNumberChooserTask) and then send an SMS (SmsComposeTask). Two or more tasks have to be used together in this case. Fortunately, the process is as easy as defining a phone task inside the Completed event handler of another task!

C#
PhoneNumberChooserTask contactsTask = new PhoneNumberChooserTask();   
contactsTask.Completed += (s, evt) =>   
{   
   if (evt.Error == null && evt.TaskResult == TaskResult.OK)   
   {   
      SmsComposeTask smsTask = new SmsComposeTask();   
      smsTask.Body = "Insert text from your application here.";   
      smsTask.To = evt.PhoneNumber;   
      smsTask.Show();   
   }   
};   
contactsTask.Show();

Demo

Finally, you can download a complete demo which uses all of the above Launchers and Choosers in a single Windows Phone application. Enjoy!

Resources

Note: There are a few more tasks which are absent from this blog post (MarketplaceDetailTask, MarketplaceHubTask, MarketplaceReviewTask and MarketplaceSearchTask specifically). These tasks are used exactly like the ones presented here, but I have excluded them from my demo because the Marketplace is not yet finalized.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
mr_vishrut17-Apr-12 7:03
mr_vishrut17-Apr-12 7:03 
Superb article.Vangos has explained everything in very simple way. Kudos to Vangos.
QuestionSetAlarmTask?? Pin
John Hoekstra20-Dec-10 3:59
John Hoekstra20-Dec-10 3:59 
AnswerRe: SetAlarmTask?? Pin
Vangos Pterneas20-Dec-10 5:53
professionalVangos Pterneas20-Dec-10 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.