다음을 통해 공유


How to use the photo chooser task for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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 for Windows Phone 8.

If you are developing a Windows Phone OS 7.1 app, debugging the photo chooser task is not supported on the Windows Phone OS 7.1 emulator. You must use a physical device. To test the photo chooser task on a physical Windows Phone OS 7.1 device, you must use the Connect Tool to launch your application. For more information, see How to test apps that use the photo chooser or camera capture task for Windows Phone 8.

On Windows Phone 8, the photo chooser task can be tested on the emulator or on a physical device using Visual Studio. If you are testing on the emulator, you should open the Photos Hub once after the emulator starts running before beginning your test. This ensures that there are sample pictures available to choose.

 Windows Phone OS 7.1: If an app that targets Windows Phone OS 7.1 is deployed to a phone running Windows Phone 8 and that app uses the photo chooser task, the system will create a directory in the top level of the app’s isolated storage called “PlatformData”. So, if your app iterates over the contents of Isolated Storage and you wish to skip over directories created by the system, skip over “PlatformData” and “Shared”.

To use the photo Chooser task

  1. Add the following statement to your code.

    using Microsoft.Phone.Tasks;
    
    Imports Microsoft.Phone.Tasks
    
  2. Declare the task object. It must have page scope, so declare it in your page before the constructor.

    PhotoChooserTask photoChooserTask;
    
    Dim photoChooserTask As PhotoChooserTask
    
  3. 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.

    photoChooserTask = new PhotoChooserTask();
    photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    
    photoChooserTask = new PhotoChooserTask()
    AddHandler photoChooserTask.Completed, AddressOf photoChooserTask_Completed
    
  4. 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.

    
    
    photoChooserTask.Show();
    
    
    
    
    
    
    photoChooserTask.Show()
    
    
    
    
    
  5. 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 8.

    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;
        }
    }
    
    Private Sub photoChooserTask_Completed(sender As Object, e As PhotoResult)
    
        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.
            'Dim bmp As System.Windows.Media.Imaging.BitmapImage = New System.Windows.Media.Imaging.BitmapImage()
            'bmp.SetSource(e.ChosenPhoto)
            'myImage.Source = bmp
        End If
    End Sub
    

See Also

Reference

PhotoChooserTask

Completed

Other Resources

How to use the camera capture task for Windows Phone 8