Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Windows Phone 7 Launchers & Choosers: The Absolute Guide

0.00/5 (No votes)
13 Dec 2010 1  
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... ;-)

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 (Let 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here