How to: Use the Photo Chooser Task for Windows Phone
March 23, 2012
Use the photo Chooser task to enable users to select an existing photo from the phone. This task launches the Photo Chooser application. If the user completes the task, an event is raised and the event handler receives a photo in the result.
By using Choosers, you help provide a consistent user experience throughout the Windows Phone platform. For more information, see Launchers and Choosers Overview for Windows Phone.
Important Note: |
|---|
To test the photo Chooser task on a physical device, use the Connect Tool to launch your application. For more information, see How to: Use the Connect Tool for Windows Phone. |
To use the photo Chooser task
Add the following statement to your code.
Declare the task object. It must have page scope, so declare it in your page before the constructor.
Add the following code to your page constructor. This code initializes the task object, and identifies the method to run after the user completes the task.
Add the following code to your application wherever you need it, such as in a button click event. To test this procedure, you can put the code in the page constructor. This is the code to launch the task.
Add the code for the completed event handler to your page. This code runs after the user completes the task. The result is a PhotoResult object that exposes a stream containing the image data. For information about working with photo image streams, see Camera and Photos for Windows Phone.
void photoChooserTask_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { MessageBox.Show(e.ChosenPhoto.Length.ToString()); //Code to display the photo on the page in an image control named myImage. //System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); //bmp.SetSource(e.ChosenPhoto); //myImage.Source = bmp; } }
Important Note: