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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds