Choosers in Windows Phone

Introduction

My previous article explained Launcher. Now, let’s move to the "Chooser" section that is as easy as Launcher. It does however have some more codes to add and does need a little extra handling. In fact, Chooser is something that provides something in return. ;)

Chooser
Background

We have a list of Choosers that look like Launcher but in the case of a Chooser we need to create an "Event" to the handler.

Since we know Choosers are those services that return something to code whereas Launchers will not.

Before doing anything, ensure that your project includes the "Microsoft.Phone.Tasks" namespace.

Use the following procedure to use a Chooser:

Step 1: Create an object for the Chooser.
Step 2: Next, we need to define a Complete Event, that will call the Handler method.
Step 3: Call the show() method to display the Choose window.
Step 4: In the Handler Method, provide the code that you want.

Whatever Chooser will send in return is stored under e since e is the second argument of the Handler method.

List of Choosers

PhoneNumberChooserTask

In this Chooser, we get the selected Contact’s Phone Number and its Phone number.

private void button1_Click(object sender, RoutedEventArgs e)

{
    PhoneNumberChooserTask phone = new PhoneNumberChooserTask();

    phone.Completed += new EventHandler<PhoneNumberResult>(phone_Completed);

    phone.Show();

void phone_Completed(object sender, PhoneNumberResult e)

{
   MessageBox.Show(e.DisplayName + " : " + e.PhoneNumber);

}

You may get such:

PhoneNumberChooserTask

EmailAddressChooserTask

Using this, you can fetch an email address from your Contacts.

private void button2_Click(object sender, RoutedEventArgs e)
{

    EmailAddressChooserTask email = new EmailAddressChooserTask();
    email.Completed += new EventHandler<EmailResult>(email_Completed);

    email.Show();

}
void email_Completed(object sender, EmailResult e)
{

    MessageBox.Show(e.DisplayName + " " + e.Email);

}


You may get such:

EmailAddressChooserTask

PhotoChooserTask: This is an important chooser to get access to your Image Gallery. With this, you can choose the image for your purposes.

private void button3_Click(object sender, RoutedEventArgs e)

{
     PhotoChooserTask photo = new PhotoChooserTask();

     photo.Completed += new EventHandler<PhotoResult>(photo_Completed);

     photo.Show();

}

void photo_Completed(object sender, PhotoResult e)
{

    BitmapImage bitmap = new BitmapImage(); // Gallery Image stored in Bitmap, as Image control is of Bitmap Type

    bitmap.SetSource(e.ChosenPhoto);

    image1.Source = bitmap; // Image Control is of Bitmap Type

}


You may get such:

PhotoChooserTask

CameraCaptureTask: The most important chooser that allows you to play with a Camera. With this, you can capture your image from a device’s camera and further process it.

private void button4_Click(object sender, RoutedEventArgs e)

{
    CameraCaptureTask camera = new CameraCaptureTask();

     camera.Completed += new EventHandler<PhotoResult>(camera_Completed);

     camera.Show();

}

void camera_Completed(object sender, PhotoResult e)
{

    BitmapImage bitmap = new BitmapImage();

    bitmap.SetSource(e.ChosenPhoto);

    image1.Source = bitmap; // Image Control is of Bitmap Type



And, you may get such output if you are using the Emulator,

CameraCaptureTask

Conclusion

We have completed one of the major topics of Windows Phone Development. Since there are many new Choosers that are added in the Windows Phon8 SDK that will be in the queue ;). If you encounter any issues in code then just get your hands dirty with the given Source Code.


Similar Articles