Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Windows Phone 7 Launchers & Choosers: The absolute guide

, 13 Dec 2010
Rate this:
Please Sign up or sign in to vote.
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 f

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

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)

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

SmsComposeTask (Creates a new SMS message)

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

EmailComposeTask (Creates a new e-mail message)

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)

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

SearchTask (Launches search)

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

MediaPlayerLauncher (Launches Media Player)

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)

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 (Lets us select a photo)

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)

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)

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)

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

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

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!

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)

Share

About the Author

Vangos Pterneas
Product Manager LightBuzz
United Kingdom United Kingdom
Vangos Pterneas is a Microsoft Most Valuable Professional in the Kinect technology. He helps companies from all over the world grow their revenue by creating profitable software products. Vangos is the owner of LightBuzz Software agency and author of two technical books.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermr_vishrut17-Apr-12 7:03 
QuestionSetAlarmTask?? PinmemberJohn Hoekstra20-Dec-10 3:59 
AnswerRe: SetAlarmTask?? PinmemberVangos Pterneas20-Dec-10 5:53 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 13 Dec 2010
Article Copyright 2010 by Vangos Pterneas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid