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

Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in iPhoneToWP7, Patterns & Skills, WindowsPhone and tagged , , . Bookmark the permalink.

2 Responses to iPhone to Windows Phone 7: Launchers and Choosers

  1. Pingback: iPhone to WP7 | Silverlight, WPF & .NET

  2. James Ashley says:

    I can’t tell if SaveEmail and SavePhoneNumber are really launchers or choosers since they both have completed events that can be handled when you return to the app. I think to capture possible errors?

    EmailComposer is also a bit of an oddball since after you fire off your email you are automatically returned to your app. The other launchers seem to require pressing the back button to return.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>