iPhone to Windows Phone 7: Launchers and Choosers

Not all Windows Phone 7 Applications are entirely self-contained.  It is not terribly surprising that some will want to reach out and touch other applications.  There are two ways to do so:

  1. Launch (and forget) another application
  2. Choose an application to do some work and return a value

Launchers

Missile Launch iStockTo Launch an application is to “fire and forget” that application; once launched the application runs on your behalf but never returns a value to your application.  There are a limited (but powerful) number of launchers supported by Windows Phone 7

  • EmailComposeTask – Launch the email application with a new message displayed.
  • MarketplaceDetailTask – Launch the Windows Phone Marketplace client application and display the details page for the specified product.
  • MarketplaceHubTask – Launch the Windows Phone Marketplace client application.
  • MarketplaceReviewTask – Launch the Windows Phone Marketplace client application and display the review page for the specified product.
  • MarketplaceSearchTask – Launch the Windows Phone Marketplace client application and display the search results from the specified search terms.
  • MediaPlayerLauncher – Launch the media player
  • PhoneCallTask – Launch the Phone application
  • SaveEmailAddressTask – Launch the contacts application
  • SavePhoneNumberTask – Launch the contacts application to save a new contact
  • SearchTask – Launch the Web Search application
  • SmsComposeTask – Launch the SMS application
  • WebBrowserTask – Launch the Web Browser application

Choosers

a businesswoman pressing a hi-tech button (you can write your command on the button: login, enter, start, yes, no, ...) Choosers, on the other hand, allow you to start an application specifically to obtain information or retrieve the results of a user’s action within that application (such as opening the camera, and then obtaining the image taken with the camera from within your application.   This can be a very powerful way to interact with the built-in functionality of the phone.  At the moment, there are four choosers available,

  • EmailAddressChooserTask – Start the Contacts application to obtain the email address of a contact selected by the user
  • CaptureCameraTask – Start the Camera application to allow the user to take a photo from your application
  • PhoneNumberChooserTask – Start the Contacts application to obtain the phone number of a contact selected by the user
  • PhotoChooserTask – Start the Photo Chooser application to allow the user to select a photo

To see this at work, we’ll create an application that lets you experiment with some Launchers and Choosers. Begin by creating a new Phone application and on the Main page add a button as shown in this Xaml,

<Grid
   x:Name="LayoutRoot"
   Background="Transparent">
   <Grid.RowDefinitions>
      <RowDefinition
         Height="Auto" />
      <RowDefinition
         Height="*" />
   </Grid.RowDefinitions>

   <StackPanel
      x:Name="TitlePanel"
      Grid.Row="0"
      Margin="24,24,0,12">
      <TextBlock
         x:Name="ApplicationTitle"
         Text="Launchers and Choosers"
         Style="{StaticResource PhoneTextNormalStyle}" />
      <TextBlock
         x:Name="PageTitle"
         Text="Phone Number"
         Margin="-3,-8,0,0"
         Style="{StaticResource PhoneTextTitle1Style}" />
   </StackPanel>

   <Grid
      x:Name="ContentPanel"
      Grid.Row="1">
      <StackPanel
         Orientation="Vertical"
         Grid.Row="0">
         <Button
            x:Name="SendSMS"
            Content="Send SMS"
            Margin="0,5" />
      </StackPanel>
   </Grid>
</Grid>


All the action is in the code-behind page. The constructor sets up the event handler for the button and then adds an instance of a  PhoneNumberchooserTask.  This is a chooser, and so you’ll get back the value of the phone number selected. Because the operation is asynchronous you must provided a Completed event handler,

  private readonly PhoneNumberChooserTask _phoneNumberChooserTask;

 public MainPage()
 {
    InitializeComponent();
    SendSMS.Click +=
                 SendSmsClick;
    _phoneNumberChooserTask =
                 new PhoneNumberChooserTask();
    _phoneNumberChooserTask.Completed +=
                 PhoneNumberChooserTaskCompleted;
 }


To launch the chooser, you call its Show method, which we’ll do in the button click event handler,

  void SendSmsClick( object sender, RoutedEventArgs e )
 {
    _phoneNumberChooserTask.Show();
 }


Calling show starts the chooser, and all the interaction with the contacts is then automatic; you don’t write code for anything until the chosen contact’s phone number is returned. At that point, completed is raised and your event handler is invoked.  In the event handler you need to check whether the user clicked OK or something else happened that you have to handle as an error,

  if ( e.TaskResult == TaskResult.OK )
 {
    //...
 }
 else
 {
    if ( e.TaskResult == TaskResult.Cancel )
    {
       MessageBox.Show( "Sending SMS Cancelled",
           "No Number Selected",
           MessageBoxButton.OK );
    }
    else
    {
       if ( e.Error != null &&
            !String.IsNullOrEmpty( e.Error.Message ) )
       {
          MessageBox.Show(
              "Error obtaining phone number:\n" +
               e.Error.Message,
               "Fail",
               MessageBoxButton.OK );
       }
       else
       {
          MessageBox.Show(
               "Unknown error obtaining phone number",
               "Fail",
                MessageBoxButton.OK );
       }
    }
 }


Assuming you did get OK back, you are ready to compose and send the SMS message, calling on the smsCompeTask launcher to do the heavy lifting,

 if ( e.TaskResult == TaskResult.OK )
 {
    var smsComposeTask = new SmsComposeTask();
    smsComposeTask.Body = "Ping!  Pizza?";
    smsComposeTask.To = e.PhoneNumber;
    smsComposeTask.Show();
 }
 else
 {
     //...
 }

The smsComposeTask takes the text you want to send (body) and who you want to send it to (the phone number, which we extract from the PhoneNumberResult) and you invoke it by calling Show().

Previous | Next

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in iPhoneToWP7, Patterns & Skills, WindowsPhone and tagged , , . Bookmark the permalink.

One Response to iPhone to Windows Phone 7: Launchers and Choosers

Comments are closed.