Sudhindra Kovalam's Blog

My Geeky Blog

Windows Phone 7 : Launcher and Choosers : Part 2

leave a comment »

Let us continue our exploration of Windows Phone 7 Launchers and Choosers.

I had described Windows Phone 7 launchers here.

Chooser is an API , that launched a  built in functionality for the user to accomplish some task and returns back the data that was chosen by the user to the app.

We had seen how we could do a new launcher as and where needed, But in case of Choosers, the game is a little different.

A Chooser starts a different application ( Your app gets tombstoned)  and after the intended action is complete, ( Your app is back from the grave mwahahah!! ) the results are returned to your application. So we will have to declare the Chooser objects at a global scope within the PhoneApplicationPage class and  instantiate  it  in the page’s ctor and do the necessary plumbing (By that, i mean handling the necessary events etc.)

As of now following choosers are exposed

1. CameraCaptureTask:

The Camera Capture Task starts the camera of the phone. Now, the user is  actually seeing the phone’s camera app, where s/he can click  photos. Once the Photo is taken , we can save this photo in the Phone’s Media library. For doing this, we need to add a reference to XNA.Framework assembly.

using Microsoft.Xna.Framework.Media;
public partial class page2 : PhoneApplicationPage
{
   CameraCaptureTask cameraCaptureTask;

   // Constructor
   public page2()
   {
      InitializeComponent();
      // Initialize the CameraCaptureTask and assign the Completed handler in the page constructor.
      cameraCaptureTask = new CameraCaptureTask();
      cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
   }

   private void btnCamera_Click(object sender, RoutedEventArgs e)
   {
      cameraCaptureTask.Show();
   }
   void objCameraCapture_Completed(object sender, PhotoResult e)
   {
       if (e.TaskResult == TaskResult.OK)
       {
          MediaLibrary library = new MediaLibrary();
          Random r = new Random(1234);
          Picture pic = library.SavePicture("SavedPicture_" + r.Next(11, 99).ToString() + ".jpg", e.ChosenPhoto);
       }
    }
}

ScreenShots:
Picture Capture Screen Accept or Retake the Pic


2. PhotoChooserTask:

As the Name suggests, the choosers shows you the WP7 picture library,from where you are allowed to pick a photo, in the completed event,
you receive a PhotoResult object that contains the Stream representation of the photo Chosen.
You Can do what you want to with the Photos’ File stream.
(Such as displaying it, uploading it to some service provider etc etc)

public partial class Page2 : PhoneApplicationPage
{
     PhotoChooserTask objPhotoChooser;
     public Page2()
     {
            InitializeComponent();
            objPhotoChooser = new PhotoChooserTask();
            objPhotoChooser.Completed += new EventHandler<PhotoResult>(objPhotoChooser_Completed);
     }
     private void btnChosePhoto_Click(object sender, RoutedEventArgs e)
        {            objPhotoChooser.Show();        }

     void objPhotoChooser_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
                MessageBox.Show("Photo Selected");
        }
}

ScreenShots:

Media Library : Album ViewPhoto List shown in a pivot

3. SavePhoneNumberTask:

This Chooser, as  the name suggests, saves a phone number in the Phone’s contacts. This chooser does not return anything. The Completed event receives argument stating the status of the operation.

Note : You need to set the PhoneNumber property before you call the Choosers show() method. (Else your code might not work)

</span>
<pre>public partial class Page2 : PhoneApplicationPage
{
     SavePhoneNumberTask objSavePhoneNumber;
     public Page2()
     {
            InitializeComponent();
            objSavePhoneNumber = new SavePhoneNumberTask();
            objSavePhoneNumber.Completed += new EventHandler<TaskEventArgs>(objSavePhoneNumber_Completed);
     }
     private void btnSavePhoneNumber_Click(object sender, RoutedEventArgs e)
     {
            objSavePhoneNumber.PhoneNumber = "123-345-4567";
            objSavePhoneNumber.Show();
     }

     void objSavePhoneNumber_Completed(object sender, TaskEventArgs e)
     {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show("Phone Number Saved..");
            }
     }
}

Screenshots:
Saving a phone number for a existing contact Phone Number Type

4. PhoneNumberChooserTask

PhoneNumberChooserTask Allows you to pick a phone number from your contacts. If the contact has multiple phone numbers, you get to pick one of the phone numbers ( Isn’t that quite handy)

public partial class Page2 : PhoneApplicationPage
{
     PhoneNumberChooserTask objPhoneNumberChooser;
     public Page2()
     {
            InitializeComponent();
            objPhoneNumberChooser = new PhoneNumberChooserTask();
            objPhoneNumberChooser.Completed += new EventHandler<PhoneNumberResult>(objPhoneNumberChooser_Completed);
     }
     private void btnChoosePhoneNumber_Click(object sender, RoutedEventArgs e)
     {
            objPhoneNumberChooser.Show();
     }

     void objPhoneNumberChooser_Completed(object sender, PhoneNumberResult e)
     {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show("Selected :" + e.PhoneNumber);
            }
     }
}

Screenshots:

5. SaveEmailAddressTask

As the name suggests, this choosers saves an email to the contact list.

6. EmailAddressChooserTask

As the name suggests, this choosers shows you the contact list from where , you can chose a  contact and your app will receive the chosen contact’s default email address. Again, if multiple email addresses are available, the user is presented a choice of picking up an email address. This is sent back to your app.

Source Code for the sample Launcher and Chooser Demo,( whose screenshots are visible above )

Once Again, thanks for your support by reading this post. Keep watching this space for more on windows Phone 7.

Next Post : Building your twitter client for Windows Phone 7 with oAuth :)

About these ads

Written by sudheerkovalam

August 14, 2010 at 9:01 am

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: