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

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds