Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 17 – Working with Choosers in WP7

 

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.  [more]

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

  • Creating an instance of the task
  • Identifying the callback method to run after the task completes
  • Setting the parameters to organize the task
  • Calling the show method to invoke the task
  • Implementing the completed event handler to get the data and status.

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:

  • Email Address Chooser Task – This task is used to launch the Contact application to select a particular contact’s email address to do some manipulations within the application from which this task is called.
  • Save Contact Task – This task is used to launch the contact application in order to save the contact details of a contact to the contact details section.
  • Save Email Address Task – This task is used to launch the contact application in order to save an email address of a contact to the contact details section.
  • Save Phone Number Task – This task is used to launch the contact application in order to save a phone number to the contact details from the application which triggered this task.
  • Camera Capture Task – This task is used to launch the in-built camera application to capture a photo and use it with the application from which the task is called.
  • Game Invite Task – This task is used to launch an invite screen for a multi player games application. This invitation will be sent asynchronously so that the task makes use of the invitation response to get connected for playing games across the sessions.
  • Address Chooser Task – This task is used to launch the Contact application to select a particular contact’s physical address selected by the user to do some manipulations within the application from which this task is called.
  • Photo Chooser Task – This task is used to launch the Photo Chooser application using which we can select a photo from the hub and use it for manipulations within the application from which we can trigger this task.
  • Phone Number Chooser Task – This task is used to launch the contact application to select a particular contact’s phone number to do some manipulations within the application from which this task is called.
  • Save Ringtone Task – This task is used to save an audio file as a ringtone to the system’s ringtone list if the audio file meets the requirement for it to be considered as a ringtone. This task will launch the ringtone application and set the ringtone properties to be used within the device profile or within some specific group or a contact.

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.

clip_image002

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:

01

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

02

<Grid.RowDefinitions>

03

<RowDefinition Height="Auto"/>

04

<RowDefinition Height="*"/>

05

</Grid.RowDefinitions>

06

 

07

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

08

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

09

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

10

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

11

</StackPanel>

12

 

13

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

14

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

15

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

16

</Grid>

17

</Grid>


Code Behind:

01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Net;

05

using System.Windows;

06

using System.Windows.Controls;

07

using System.Windows.Documents;

08

using System.Windows.Input;

09

using System.Windows.Media;

10

using System.Windows.Media.Animation;

11

using System.Windows.Shapes;

12

using Microsoft.Phone.Controls;

13

using Microsoft.Phone.Tasks;

14

 

15

namespace F5debugWp7Choosers

16

{

17

public partial class MainPage : PhoneApplicationPage

18

{

19

PhoneNumberChooserTask pnChooserTask;

20

 

21

// Constructor

22

public MainPage()

23

{

24

InitializeComponent();

25

 

26

pnChooserTask = new PhoneNumberChooserTask();

27

pnChooserTask.Completed += new EventHandler(pnChooserTask_Completed);

28

}

29

 

30

void pnChooserTask_Completed(object sender, PhoneNumberResult e)

31

{

32

PhoneCallTask phoneCallTask = new PhoneCallTask();

33

phoneCallTask.PhoneNumber = e.PhoneNumber;

34

phoneCallTask.Show();

35

}

36

 

37

private void button1_Click(object sender, RoutedEventArgs e)

38

{

39

pnChooserTask.Show();

40

}

41

}

42

}

clip_image004

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.

clip_image006

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter