31 Days of Windows Phone | Day #8: Choosers

This post is Day #8 in a series called the 31 Days of Windows Phone.

Yesterday I discussed how to use Launchers in a Windows Phone application.  Today, we’re going to look at a similar piece of functionality for your apps, the Chooser.

What are Choosers?

Choosers are a little more complicated than Launchers, because they return data to your application, where a Launcher just gives the user a new task to accomplish.  We’ll cover each of the Choosers available to you as a developer on this platform.  At the end of this post, there is a downloadable project that includes code samples for each of the Choosers described here.

For each of these Choosers, you will also need to make sure that you have this statement at the top of your code-behind file.  We need a reference to Microsoft.Phone.Tasks.

using Microsoft.Phone.Tasks;

For a quick look at the list of Choosers, here’s what you’ve got so far:

  • CameraCaptureTask – gives your user the ability to take a picture directly from your application.
  • EmailAddressChooserTask – allows your user to select an email address from their contacts to use in your application.
  • PhoneNumberChooserTask – allows your user to select a phone number from their contacts to be used in your application.
  • PhotoChooserTask – allows your user to select a photo from their device to be used in your application.

Using a Chooser

Each of the Choosers have their own set of properties, but after setting any of them up, you need to .Show() them to the user.  To do this, you simply call the Show() method on your Chooser.  The important thing to notice between these and the Launchers is that you need to create an event handler for when they make their selection.  Otherwise, you won’t be able to grab the values they choose.

As an example, here’s the CameraCaptureTask:

CameraCaptureTask cct = new CameraCaptureTask();
cct.Completed += new EventHandler<PhotoResult>(cct_Completed);
cct.Show();

And here’s my event handler code:

void cct_Completed(object sender, PhotoResult e)
{
	BitmapImage bmp = new BitmapImage();
	bmp.SetSource(e.ChosenPhoto);
	image1.Source = bmp;
}

You’ll notice that I created an event handler for when the user has taken a picture.  This is so that I can get that image data into my application, and use it.  When you try this in the emulator, you’ll notice that it doesn’t actually take advantage of a webcam.  Instead, it creates a fake image that has a black block rotating around a white canvas.  This should suffice for your testing, but I’d still recommend using actual hardware before deploying your app to the marketplace.

I linked each of the Chooser names above to their MSDN articles, where you can see the properties of each.  It didn’t make much sense for me to replicate the great work they’re doing over there.

Tomorrow, we’re going to talk about some of the tools that are available to you when you’re debugging in Windows Phone 7.

Download the Code

I have included a simple project that uses each of the above Choosers in an example.

download

7 thoughts on “31 Days of Windows Phone | Day #8: Choosers

  1. hello, I come from China, like your blog

  2. Hell is full of musical amateurs music is the brandy of the damned.

  3. The glory of friendship is not the outstretched hand, nor the kindly smile, nor the joy of companionship it is the spiritual inspiration that comes to one when you discover that someone else believes in you and is willing to trust you with a friendship.

  4. It's all that the young can do for the old, to shock them and keep them up to date.

  5. Winning is not a sometime thing it's an all time thing. You don't win once in a while, you don't do things right once in a while, you do them right all the time. Winning is habit. Unfortunately, so is losing.

  6. The capturing works fine on the emulator but not on device when it is connected. So when disconnecting the Windows Phone (Nokia Lumia 800) it works. But how is it possible to debug certain stuff then?

    Cheers.

    • When debugging the use of the camera, you are somewhat limited. The emulator should give you everything you need for debugging, so that’s what I would use. If you absolutely need to see it capture a “real” image (rather than the generated one that the emulator provides, you will have to disconnect. When your phone is plugged into your computer, the camera is disabled on the device.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s