Introduction

In this article we are going to see what Choosers are in Windows Phone 7 development and the different type of chooser tasks available. In our earlier article we have seen about Launchers and the different tasks involved with the launchers and a sample application on how to use the Launchers. Similarly here in this article we will see about choosers in detail.

Choosers in Windows Phone 7 is an API used to launch some of the built in applications using which the user can perform some task related to the application launched. Unlike launchers, here using choosers we can perform some task and pass data to get the task completed as per the requirement. Basically the choosers will be supplied with some data and status to do some manipulations accordingly. Some of the example of using the choosers are selecting particular photo from the photo hub, saving a ringtone etc.

Choosers have some general steps to launch a particular application and steps are as follows

Let us see the list of Choosers available and the usage of each and every chooser tasks to get some clear idea on when to use each task to get a better performance.

Chooser Tasks

Now let us take a small example on how to use one of the Chooser task and in our upcoming articles we will see in depth on each and every task. Let us create a Phone number chooser application to select a contact and make a call.

Steps

Open Visual Studio 2010 and create a new Silverlight for Windows Phone 7 Application with a valid project name as shown in the screen below.

img10.gif

Now let us add a button to invoke the Phone Number Chooser task and call a select a particular contact number, write the below code on the click event of the button as shown in the screen below. Also note that we need to add the below Using Directive in order to use the PhoneNumberChooser task (using Microsoft.Phone.Tasks; ) PhoneNumberChooser Task invokes the contacts application to get the selected contact by the user. The chooser works asynchronously so that the completed event must be subscribed before launching the chooser. Once the chooser task returns the Phone Number then the PhoneCallTask is used to make the phone call to the user selected Phone number as shown in the code.

XAML Code

<Grid x:Name="LayoutRoot" Background="Transparent">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

<TextBlock x:Name="PageTitle" Text="Choosers" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel>

<!--ContentPanel - place additional content here-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<Button Content="Choose Contact" Height="189" HorizontalAlignment="Left" Margin="74,85,0,0" Name="button1" VerticalAlignment="Top" Width="297" Click="button1_Click" />

</Grid>

</Grid>

Code Behind

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Microsoft.Phone.Controls;

using Microsoft.Phone.Tasks;

namespace F5debugWp7Choosers

{

public partial class MainPage : PhoneApplicationPage

{

PhoneNumberChooserTask pnChooserTask;

// Constructor

public MainPage()

{

InitializeComponent();

pnChooserTask = new PhoneNumberChooserTask();

pnChooserTask.Completed += new EventHandler(pnChooserTask_Completed);

}

void pnChooserTask_Completed(object sender, PhoneNumberResult e)

{

PhoneCallTask phoneCallTask = new PhoneCallTask();

phoneCallTask.PhoneNumber = e.PhoneNumber;

phoneCallTask.Show();

}
private
void button1_Click(object sender, RoutedEventArgs e)

{

pnChooserTask.Show();

}

}

}

a1.gif

Now we are done with the code, build and execute the project by pressing F5 and we can see the application opened in Windows Phone 7 Emulator as shown in the screens below.

a2.gif

Conclusion

So in this article we have seen what Choosers are in Windows Phone 7 development and the list of Choosers available. Also we have seen a sample on What PhoneNumberChooser Task is and how to use it practically with the Windows Phone 7 application development.