Thursday, September 16, 2010

Windows Phone 7 Quick Tip #4 – Choosers and OnNavigatedTo/Loaded Methods

Here’s a tidbit of knowledge I picked up while working with the Camera Chooser.  After your application comes back from taking a picture, you can get the picture or find out the user canceled with the following chunk of code:

_cameraCaptureTask.Completed += (sender, args) =>
{
      if (args.TaskResult == TaskResult.OK)
      {
           _image = new BitmapImage();
           _image.SetSource(args.ChosenPhoto);
           CameraButton.Source = _image;
           CameraButton.Stretch = System.Windows.Media.Stretch.Uniform;
        }
};

I ran into a problem where I was initializing the page in the OnNavigatedTo overload.

It turns out for this instance this was the wrong place to do this.  The reason being the cameraCaptureTask completed and then the OnNavigatedTo method was called.  I guess this makes sense if you think about it.  I put the initialization “stuff” in the Loaded event and everyone was happy.

So the point behind this quick tip is to make sure you are initializing your page in the proper sequence.  When coming back to your page, the OnNavigatedTo method is called, but the Loaded event isn’t fired.  In addition the chooser completion event is fired before the OnNavigatedTo method is called.

Of course the big and burning question you are asking yourself is how does tombstoning come into play after a chooser is displayed and dismissed.  Stay tuned for a future Windows Phone 7 Quick Tip on the Cliff Notes for Tombstoning.

-twb

No comments:

Post a Comment