Advertisment

Windows Phone 7: Launchers & Choosers

author-image
PCQ Bureau
New Update

Last month in this series on developing your own apps forWindows Phone 7 we looked at interacting with some ofthe cool new user interface elements introduced in thisplatform. The Pivot and Panorama controls bring a fresh newuser experience in using a smart phone to the user. This monthwe will delve into how you can interact with other built-in appli-cations and services available in the phone from your app to usethe data or feature within them.Windows Phone 7 does this by exposing the phone servicesusing Launchers and Choosers. There are a number of differentLaunchers and Choosers built into the system that can be calledfrom the application. But what's the difference between these?A Launcher allows your application to launch a differentservice in the phone and execute that. It does not return any databack to the calling application. A Chooser on the other handlaunches a different service, allows the user to pick some datafrom that and return that data back to the calling application.Here is a list of all the available Launchers and Choosers in Win-dows Phone 7.

Advertisment

Launchers

●PhoneCallTask

Advertisment

●SMSComposeTask

●WebBrowserTask

●SearchTask

Advertisment

●EmailComposeTask

●MediaPlayerLauncher

●MarketplaceDetailTask

Advertisment

●MarketplaceHubTask

●MarketplaceReviewTask

●MarketplaceSearchTask

Advertisment

●EmailAddressChooserTask

●PhoneNumberChooserTask

●PhotoChooserTask

Advertisment

●SaveEmailAddressTask

●SavePhoneNumberTask

●CameraCaptureTask

Advertisment

Choosers

In both cases, youapplication termi-nates when thetask is called andthen is reactivated.See the details ofthe launching, activation, tombstoning application lifecycleevents in part 2 of thisseries for details onhow the applicationcomes back.

Due to the application life cycle there are afew other things to keep in mind — youneed to declare a the setasks up as a classscope object in the application page that you are calling them from. In the case of a Chooser, you will also need to declare anevent handler for the Completed event that needs to fire as soonas the choice is made and your application is reactivated by thephone. All of thisneed to be donein the constructor of the page sothat WindowsPhone 7 can keepa track of the del-egate. So let'slook at somecode sample towalk through.

Let's first create asmall app page thatlets the user select aphoto or image fromthe phone's Photo hub.We will use the Photo-ChooserTask for this.In a new applicationpage, declare the fol-lowing at the classscope.

PhotoChooserTaskphotoChooserTask;Next, in the page'sconstructor, add the following code:public MainPage(){InitializeComponent();photoChooserTask = new PhotoChooserTask();photoChooserTask.Completed +=new Even-tHandler(PCT_Completed);}

The above code creates a delegate for handling the eventwhen the app is reactivated after the user selects a photo. Fi-nally, create the handler itself.

void PCT_Completed(object sender, PhotoResult e){if (e.TaskResult == TaskResult.OK){BitmapImage bmp = new BitmapImage();bmp.SetSource(e.ChosenPhoto);ImageBrush i = new ImageBrush();i.ImageSource = bmp;this.LayoutRoot.Background = i;}}

This code simply changes the background of the current ap-plication page to the chosen photo. You obviously need to startthe photo task — say from a button's click event like this:

private void button1_Click(object sender, RoutedEventArgs e){photoChooserTask.Show();}

This will allow a user to click the button, select a photo and return where the application will now use the selected photo asthe page background. This was an example of a chooser. For a sample of a launcher, we can create a simple messagesending input. Add the following at the class scope:

PhotoChooserTask

photoChooserTask;

Next, in the page's constructor, add the following code:

public MainPage()

{

InitializeComponent();

photoChooserTask = new PhotoChooserTask();

photoChooserTask.Completed +=new EventHandler<

PhotoResult>(PCT_Completed);

}

The above code creates a delegate for handling the event when the app is reactivated after the user selects a photo. Finally, create the handler itself.

void PCT_Completed(object sender, PhotoResult e)

{

if (e.TaskResult == TaskResult.OK)

{

BitmapImage bmp = new BitmapImage();

bmp.SetSource(e.ChosenPhoto);

ImageBrush i = new ImageBrush();

i.ImageSource = bmp;

this.LayoutRoot.Background = i;

}

}

This code simply changes the background of the current application page to the chosen photo. You obviously need to start the photo task — say from a button's click event like this:

private void button1_Click(object sender, RoutedEventArgs e)

{

photoChooserTask.Show();

}

This will allow a user to click the button, select a photo and return where the application will now use the selected photo as the page background. This was an example of a chooser.

For a sample of a launcher, we can create a simple message sending input. Add the following at the class scope:

PhoneNumberChooserTask phoneTask;

And this in the constructor:

phoneTask = new PhoneNumberChooserTask();

phoneTask.Completed += new EventHandler<

PhoneNumberResult>(phoneTask_Completed);

And the event handler itself:

void phoneTask_Completed(object sender, PhoneNumberResult

e) {

if (e.TaskResult == TaskResult.OK)

{

SmsComposeTask sms = new SmsComposeTask();

sms.Body = textBox1.Text;

sms.To = e.PhoneNumber;

sms.Show();

}

else if (e.TaskResult == TaskResult.Cancel)

MessageBox.Show("Please select contact for phone number.",

"No Number", MessageBoxButton.OK);

else

Messa

Box.Show("Some Error");

}

A textbox and a button with an event handler with phone-Task.Show(); in the code gets you a nice little SMS composescreen. In this the user can enter some text in the textbox, clickthe button to select a phone number from his contacts list andthen move to send the SMS directly. The above code showcasesthe PhoneNumberChooserTask Chooser and the SMSCompose-Task Launcher working in conjunction with each other. As youcan see, interacting with the built-in phone services and data isquite easy. You can create complete new services or interact withthem from within your application and provide the user a con-sistent user interface all across.

Advertisment