WP7 Development Tip of the Day: Navigating between pages: NavigateUri vs. NavigationService

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

Tags:

Windows Phone | WP7 Tip of the Day

Comments

10/19/2010 11:13:41 AM #

trackback

WP7 Development Tip of the Day: Which UriKind to use when navigating using NavigationServer.Navigate

WP7 Development Tip of the Day: Which UriKind to use when navigating using NavigationServer.Navigate

code badger

10/19/2010 2:55:32 PM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        WP7 Development Tip of the Day: Navigating between pages: NavigateUri vs. NavigationService
        [codebadger.com]
        on Topsy.com

topsy.com

10/20/2010 2:54:01 PM #

trackback

Windows Phone 7: Experiência para programadores (XL)

Aqui vai mais uma série de linksp para artigos relacionados com o desenvolvimento para WP7 e outras questões

Alberto Silva

Comments are closed

About Brad

Brad Tutterow lives in Illinois and works in Missouri. He has 12 years of experience developing web sites and Windows applications using a variety of technologies and is most excited currently about Silverlight, Windows Phone 7, Halo Reach, and Visual Studio 2010.