Launchers & Choosers–Part 2

Windows Phone From Scratch #23SMS

In yesterday’s posting we created an application that used a chooser to allow you to pick a name from the contacts list and to obtain the related phone number.

Today we’ll use that information to launch the SMS service to send a message to the selected user.

Today’s posting is very short, as the work of creating a launcher is nearly identical to that of calling a chooser, except that with a launcher you don’t get information back.

You will remember that yesterday we created a private member variable of type PhoneNumberChooserTask.  Today we’ll add a second private member of type SmsComposeTask

 private PhoneNumberChooserTask _choosePhoneNumber;
 private SmsComposeTask _smsCompose;

This time we’ll initialize the Task in the call-back method for the phone number, thus creating a clear logic to the program:

  • Ask for the phone number
  • When you return with the phone number feed it to the SMS Task
  • Launch the SMS Task.

The SmsComposeTask has two properties: the body of the message and the phone number. We’ll add these one by one (though we could just use member initialization) and then, as we did with the Chooser, we’ll call Show to start up the Launcher.  The difference is that there is no call back, as Launchers do not return information.

Here is the complete code behind  – there are no changes to the Xaml:

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace SMS
{
   public partial class MainPage : PhoneApplicationPage
   {
      private PhoneNumberChooserTask
         _choosePhoneNumber;
      private SmsComposeTask _smsCompose;

      public MainPage()
      {
         InitializeComponent();
         _choosePhoneNumber =
            new PhoneNumberChooserTask();
         _choosePhoneNumber.Completed +=
            new EventHandler<PhoneNumberResult>(
               _choosePhoneNumber_Completed );
         SMS.Click +=
            new RoutedEventHandler( SMS_Click );
      }

      void SMS_Click(
         object sender,
         RoutedEventArgs e )
      {
         _choosePhoneNumber.Show();
      }

      void _choosePhoneNumber_Completed(
         object sender,
         PhoneNumberResult e )
      {
         _smsCompose = new SmsComposeTask();
         _smsCompose.Body =
            "Sending SMS message...";
         _smsCompose.To = e.PhoneNumber;
         _smsCompose.Show();
      }
   }
}

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 Patterns & Skills and tagged , . Bookmark the permalink.

One Response to Launchers & Choosers–Part 2

Comments are closed.