How To Use Choosers in Your Windows Phone 7 Applications

Introduction

As we saw in an earlier article describing Launchers, the Windows Phone 7 platform does not provide any application direct access to the contacts, send email or take pictures features of the phone. This is done to provide sandboxing. However when applications have a legitimate need for doing so, they can access these features via some helper APIs. When there is a need for output from such platform specific tasks, we use Chooser APIs, which are provided by the Windows Phone 7 platform.

Choosers Defined

Choosers are a set of APIs that launche one of the built-in applications by which users can complete a task for which the Windows Phone 7 platform provides a native application. In addition to doing the task, a Chooser API also returns a value, which is used by the application to determine business logic. An example would be selecting a phone number.

Developers will author their applications such that their application code calls the Chooser API with the required information and the Choosers API will do the specified task. For example, you can call a Chooser API to select a contact's phone number to populate a field in the current application. When the user selects a contact, the Chooser API returns the phone number of the chosen contact to the originally executing application.

Types of Choosers

There are various Choosers available with Windows Phone 7:

  • For camera capture
  • For choosing email address
  • For choosing phone number
  • For choosing photo
  • For saving email address
  • For saving phone number

Let's look at how we can use these Choosers in an application.

Hands-On

Let us start by creating a new Silverlight C# project for Windows Phone. Let's call it ChooserDemo.

Create a new Silverlight C# project
Figure 1: Create a new Silverlight C# project

Add a button with the text "Choose Phone number". Also add a text box on the MainPage.

In the MainPage.xaml.cs, add a reference directive for Microsoft.Phone.Tasks namespace.

In the class for the MainPage, declare an object of type PhoneNumberChooserTask.

PhoneNumberChooserTask phoneNumberChooserTask;

In the constructor for MainPage class, instantiate the PhoneNumberChooserTask.

Wire a method called phoneNumberChooserTask_Completed on its Completed Event.

        public MainPage()
        {
            InitializeComponent();

            phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);
 
        }

On the button click event, add the following code.

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

        }

Now, lay out the phoneNumberChooserTask method. In this method, update the textbox to show the phone number ifB a contact is selected.

void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                // if successfully chose a number, show it.
                textBox1.Text = e.PhoneNumber;
            }
        }

We are all set. Go ahead and launch the application. If you are having issues compiling, you can grab the source code of the demo, which is referenced in the resources section of this article.

When you click the "Choose Phone number" button, the address book opens up and once you choose a contact from that, it automatically fills the textbox on the MainPage with that contact's phone number.

You have successfully used the Chooser feature of Windows Phone 7 platform. As simple as that.

Summary

In this article, we learned how to use Choosers API.B

About the author

Vipul Patel is a Software Engineer currently working at Microsoft Corporation. He is currently working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com



About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Do you know where your data is? Consumer cloud-based file sharing services store your sensitive company data on servers outside of your control, outside of your policy and regulatory guidelines – maybe even outside your country – and not managed by you. The potential for data leakage, security breaches, and harm to your business is enormous. Download this white paper to learn about file sync and share alternatives that allow you to manage and protect your sensitive data while integrating and …

  • If you need new tools and tricks to make your meetings profitable and productive, then 5 Tips in 5 Minutes: A Quick Guide for More Profitable Sales Meetings is for you. Timely, practical tips that you can incorporate in just seconds will save you literally hours in travel and meeting time, not to mention help you to focus on what your sales prospects really want to know and how you can meet their needs. Get 5in5: A Quick Guide for More Profitable Sales Meetings and start building your sales the smarter, faster …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds