Skip to content

Windows Phone 7: Navigating After A Picture/Camera Task

UPDATE: There are a number of other possible solutions in the comments.

While working on a Windows Phone 7 project I came up against a problem in which the project would crash after we took a picture, giving us the error:

NavigationService.Navigate(new Uri(“/OurView/NextView.xaml”, UriKind.Relative));

The scenario we wanted went something like this: We wanted the user to tap a button and launch the camera task. This would close (tombstone) the application until the camera task was complete (the user took a picture and returned to the app). Upon completion, the application is re-initiated and the camera task hands the application an image stream that we can use to determine the bar code number. We then navigate to a screen that will show all the information about the product the user just scanned.

The problem with this way of doing things is that when we tried to navigate to a new page in our camera.Completed event handler (helpfully titled camera_Completed), we got a NullReferenceException on our NavigationService.

“How could this be?” I asked myself and the Internet. The NavigationService was there when we left. How could it not be there when we got back? The Internet had no helpful answers. Stupid Internet.

As it turns out, the NavigationService doesn’t actually show up in a page until the page UI is fully loaded (or so I have been told). That means that we simply can’t navigate directly from the camera.Completed event because the UI isn’t loaded.

Most other Windows Phone 7 programs with camera tasks we looked at had some additional user input after the task was completed that users would then interact with to handle navigation. We didn’t want that. Once the user took a picture, we wanted them to start getting results without any additional interactions.

Here is our solution for navigating after the camera task (which will also work for navigating after any task):

First, add a property to the page that can determine if the application was initiated after the task.

bool comingFromCamera = false;

Make sure you set this to true in your camera.Completed event handler:

void camera_Completed(object sender, PhotoResult e)
{
this.comingFromCamera = true;
}

Add an event handler for the page loading event  in the page constructor:

public MainPage()
{
InitializeComponent();
// Whatever other stuff you need in here
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

In the MainPage_Loaded event handler, check to see if the page was initialized after the camera task or initialized normally. If it was initialized after the task, navigate to your heart’s content.

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
if(this.comingFromCamera)
NavigationService.Navigate(new Uri(“/OurView/NextView.xaml”), UriKind.Relative));
}

And that should work. Let me know in the comments if you’re having additional difficulties.