by brad
19. October 2010 06:00
Navigation is super easy in Windows Phone 7, thanks to a frame/page model that works much like the web paradigm of navigating. This model comes from the full Silverlight framework and carries over quite nicely to the phone.
There are three basic ways to navigate between pages in your app.
XAML
The easiest way to set up navigation is the all-XAML approach. Create a hyperlink button on one page and set the NavigateUri property to the name of the page that you want to navigate to.
1: <HyperlinkButton Content="HyperlinkButton" NavigateUri="/SecondPage.xaml"/>
The Uri above is for a page called “SecondPage.xaml” that is in the root folder of the project. This is a convenient way to navigate, but is limited to Hyperlinkbuttons. Other controls (such as buttons) do not have a NavigateUri property so can’t be used in this way.
NavigationService.Navigate()
Another way to accomplish the same navigation as above is to write C# code and an event handler for the Hyperlinkbutton’s Click event.
1: private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
2: {
3: NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
4: }
While a bit more typing than the XAML-only approach, this will work for a variety of controls, such as buttons, items in a list, etc.
NavigationService.GoBack()
A third approach is to use the GoBack method of the NavigationService. This will take the user back one page in their navigation history. It’s good practice to first check if there even is a page in the navigation history since the GoBack method will throw a big fat exception if there isn’t.
1: if (NavigationService.CanGoBack)
2: {
3: NavigationService.GoBack();
4: }
Other Resources
More Tips