Continuing my mini-series on Windows Phone Fundamentals, begun here, this tutorial will cover the topic of navigating from one page to another. This is one of those tasks that are wicked simple once you know how, but which are not at first obvious at all.
To see this at work, create a new Windows Phone application and name it navigation. Open MainPage.xaml and set the ApplicationTitle to Navigation and set the PageTitle to Page 1.
Create a second page by right clicking on the project file and choosing Add –> New Item. From the dialog box choose Windows Phone Portrait Page and name it Page 2.xaml. In the newly created page set the ApplicationTitle to Navigation and set the PageTitle to Page 2.
Switch back to page 1 (MainPage.xaml) and drag and drop a HyperlinkButton onto the page. In the properties window find the Content property and change it to Page 2 and then set the NavigateUri property to /Page2.xaml.
Switch to Page2.xaml and drag a button onto the page. In the xaml set its content to Page 1 and set its name to Navigate. Now let’s set up an event handler for this button’s click event. Open Page2.xaml.cs and add the following to the constructor:
Navigate.Click += Navigate_Click;
In the stubbed out Navigate_Click method replace the exception with the following code:
NavigationService.Navigate( new Uri( "/MainPage.xaml", UriKind.RelativeOrAbsolute ) );
Build and run your application, you should now be able to navigate back and forth between the two pages.