Advertisment

Windows Phone 7: Launchers & Choosers

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: Last month in this series on developing your own apps for Windows Phone 7 we looked at interacting with some of the cool new user interface elements introduced in this platform. The Pivot and Panorama controls bring a fresh new user experience in using a smart phone to the user.

Advertisment

This month we will delve into how you can interact with other built-in applications and services available in the phone from your app to use the data or feature within them. Windows Phone 7 does this by exposing the phone services using Launchers and Choosers.

There are a number of different Launchers and Choosers built into the system that can be called from the application. But what's the difference between these? A Launcher allows your application to launch a different service in the phone and execute that. It does not return any data back to the calling application. A Chooser on the other hand launches 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 Windows Phone 7.

Launchers

Advertisment

?PhoneCallTask

?SMSComposeTask

?WebBrowserTask

Advertisment

?SearchTask

?EmailComposeTask

?MediaPlayerLauncher

Advertisment

?MarketplaceDetailTask

?MarketplaceHubTask

?MarketplaceReviewTask

Advertisment

?MarketplaceSearchTask

?EmailAddressChooserTask

?PhoneNumberChooserTask

Advertisment

?PhotoChooserTask

?SaveEmailAddressTask

?SavePhoneNumberTask

Advertisment

?CameraCaptureTask

{#PageBreak#}

Choosers

In both cases, you application terminates when the task is called andthen is reactivated. See the details of the launching, activation, tombstoning application life cycle events in part 2 of thisseries for details on how the application comes back.

Due to the application life cycle there are afew other things to keep in mind ? you need to declare a the set asks 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 soon as the choice is made and your application is reactivated by the phone. All of this need to be donein the constructor of the page so that Windows Phone 7 can keepa track of the delegate. So let'slook at some code sample to walk through.

Let's first create a small app page that lets the user select a photo or image fromthe phone's Photo hub. We will use the Photo-Chooser Task for this. In a new application page, declare the following 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 application 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();}

{#PageBreak#}

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.

{#PageBreak#}

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.

tech-news