Windows Phone From Scratch #23
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(); } } }
One Response to Launchers & Choosers–Part 2