wp7appdevelopment

Adventures in Windows Phone app development

Monthly Archives: February 2012

Using PhotoChooserTask in Silverlight/Windows Phone apps

The PhotoChooserTask launches an app that lets the user select a photo from their phone. If the user selects a photo, the app raises the Completed event and the event handler gets passes a PhotoResult object. You can access the image data through this object’s photo image stream.

    1. Include a using statement for Microsoft.Phone.Tasks in your page class (e.g, MainPage.xaml.cs):
      using Microsoft.Phone.Tasks;
    2. In your page class, include a declaration for a PhotoChooserTask:
       PhotoChooserTask photoChooserTask;
    3. In the constructor, initialize the PhotoChooserTask and add the handler for the Completed event:
      photoChooserTask = new PhotoChooserTask(); 
      photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    4. Add the Completed handler:
      // The Completed event handler. In this example, a new BitmapImage is created and  
      // the source is set to the result stream from the PhotoChooserTask
      void photoChooserTask_Completed(object sender, PhotoResult e)
          {
              if (e.TaskResult == TaskResult.OK) {
                  System.Windows.Media.Imaging.BitmapImage bmp =
                     new System.Windows.Media.Imaging.BitmapImage(); 
                  bmp.SetSource(e.ChosenPhoto);
              }
          }
    5. Add a method to call the PhotoChooserTask. This examples assumes the task will be called after a button click:
      // In this example, the PhotoChooserTask is shown in response to a button click
      private void button1_Click(object sender, RoutedEventArgs e)
      {
          try {
              photoChooserTask.Show();
          }
          catch (System.InvalidOperationException ex) {
              // Catch the exception, but no handling is necessary.
          }
      }

Note: if the PhotoChooserTask looks like it’s not working on the device but it works fine on the emulator, disconnect your phone from the Zune software. If you’re debugging while the Zune software is running, the task will return the Cancel event and never display the photo chooser.

Complete Code Sample

public partial class MainPage : PhoneApplicationPage
{
    // Declare the PhotoChooserTask object with page scope.
    PhotoChooserTask photoChooserTask;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Initialize the PhotoChooserTask and assign the Completed handler in the page constructor.
        photoChooserTask = new PhotoChooserTask();
        photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    }

    // In this example, the PhotoChooserTask is shown in response to a button click
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        try {
            photoChooserTask.Show();
        }
        catch (System.InvalidOperationException ex) {

           // Catch the exception, but no handling is necessary.
        }
    }

    // The Completed event handler. In this example, a new BitmapImage is created and
    // the source is set to the result stream from the PhotoChooserTask
    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK) {
            System.Windows.Media.Imaging.BitmapImage bmp =
                new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
         }
    }
}

Additional References

Windows Phone App Without a Tile Title

The icon for my latest Windows phone app includes the app name in the design. So, I don’t want to display the default application name on the tile when it’s pinned to the start menu. But how to remove the application title from the tile?
Turns out it’s simple: just edit the WMManifest.xml file in the Properties folder.

Find the following entry:

<Tokens> 
    <PrimaryToken TokenID="AppToken" TaskName="_default">
        <TemplateType5>
            <BackgroundImageURI IsRelative="true" IsResource="false">Background.png</BackgroundImageURI> 
            <Count>0</Count>
            <Title>YOUR APP TITLE HERE</Title>
        </TemplateType5>
    </PrimaryToken>
</Tokens>

And change the Title element to:

<Title></Title>

Done. It’s that simple. Now when I pin my app to the start page, the title is no longer displayed.