Page Navigation in Windows Phone 8 Application

This article shows how to navigate from one page to another as well as how to a parameter or value from one page to another. In the previous tutorials, we saw:
Page navigation is used to navigate from one page to another in Windows Phone 8. Navigation is managed by the NavigationService class.

The NavigationService provides the Navigate method to navigate to the content specified by a Uniform Resource Identifier (URI). The Navigate method takes a URI that is the name of the page on which you want to navigate.
 
Let's Begin
1. Navigation from one page to another

Create a New Windows Phone Application and select Windows Phone 8.0 as a target Windows Phone OS for this application and click on OK. Drop a button control into MainPage.xaml as shown in the following image and name it btn_navigationPage.



MainPage.xaml Code
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.             <Button Name="btn_navigationPage"   
  3.                     Content="Navigate between page"   
  4.                     VerticalAlignment="Top">  
  5.             </Button>  
  6. </Grid>  
Now, right-click on the project, Add, then click on New Item.

 
Add a page (name it AboutPage.xaml).

 

Add a HyperlinkButton control (named it hlb_GoBack). Design the UI of the page as in the following image.

 

AboutPage.xaml Code
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.             <StackPanel>  
  3.             <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lectus nisl, tempor sit amet sollicitudin ut, finibus   
  4.   
  5. ullamcorper nisl. Nulla facilisi." TextWrapping="Wrap"></TextBlock>  
  6.             <HyperlinkButton Name="hlb_GoBack" Content="Go Back"/>  
  7.             </StackPanel>  
  8. </Grid>  
Now, go to MainPage.xaml and double-click the btn_navigationPage button to create a click event and add the following code:
  1. private void btn_navigationPage_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.         NavigationService.Navigate(new Uri("/AboutPage.xaml",UriKind.Relative));  
  4. }  
In the preceding code, we have called the Navigate method that navigates to the content specified by the Uniform Resource Identifier. We set UriKind.Relative because AboutPage.xaml is part of the same project or XAP file. We have already added a HyperlinkButton on AboutPage.xaml. We will use this for navigating back to the previous page. (We don't need to code for the hardware back key button in Windows Phone for navigating back to the previous page.)

Double-click on it to generate its click event and add the following lines of code.
  1. private void hlb_GoBack_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.         //Navigate to most recent entry in the back navigation history  
  4.         NavigationService.GoBack();  
  5. }  
The NavigationService.GoBack() method redirects the user to the previous page.

Preview

 
 
2. parameter/value from one page to another

Let's create a new project and add a page (name it FillDetails.xaml). Drop a TextBox (named txt_Name) and Button (named btn_Submit) control onto it.

 

FillDetails.xaml Code
  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.       <StackPanel>  
  3.             <TextBlock Text="Enter your Name:"></TextBlock>  
  4.             <TextBox Name="txt_Name"></TextBox>  
  5.             <Button Name="btn_Submit" Content="Submit"></Button>  
  6.       </StackPanel>  
  7. </Grid>  
Add another page (named DisplayDetails.xaml) on which we will display the data ed from the FillDetails.xaml page. 

 
Now, go to the FillDetails.xaml page and create a click event of btn_Submit by double-clicking on it. 
  1. private void btn_Submit_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.         // Checking txt_Name is not empty  
  4.         if (txt_Name.Text != "")  
  5.         {  
  6.            NavigationService.Navigate(new Uri("/DisplayDetails.xaml?name=" + txt_Name.Text, UriKind.Relative));  
  7.         }  
  8.         else  
  9.         {  
  10.            MessageBox.Show("Please Provide Name!");  
  11.         }  
  12. }  
In the preceding code, we ed a name as a query string parameter in the URI. Open the DisplayDetails.xaml.cs code and override the OnNavigatedTo method.

The OnNavigatedTo method is called when the page becomes the active page in a frame. Get the value from the query string and display it on lbl_display using the Text property.
  1. protected override void OnNavigatedTo(NavigationEventArgs e)  
  2. {  
  3.      base.OnNavigatedTo(e);  
  4.      string name = "";  
  5.      if(NavigationContext.QueryString.TryGetValue("name"out name))  
  6.      {  
  7.           lbl_display.Text = "Hello! " + name+"\n Welcome to Windows Phone 8 Apps Development Tutorial Series.";  
  8.      }  
  9. }  
Preview

 

I hope you like this. Thanks. 


Similar Articles