Introduction 

Navigation means moving from one page to another page on a click. So today I will explain navigation from one page to another page. This article shows how to navigate to the next page with data. So let's start.

First create a Windows Phone blank app and put some controls in the MainPage.xaml. I have put only a single button for navigation but it depends on the kind of page you want to create and what controls you want to add. In my case I am using a single button control just to navigate from main page to my second page with data.

Here is the code: 
  1. <Grid>  
  2.     <StackPanel>  
  3.         <TextBlock Text="MainPage.xaml"/>  
  4.         <Button Content="Navigation Button" Name="Navigate"Click="Navigate_Click"/>  
  5.     </StackPanel>  
  6. </Grid>  
And I have added a sample class for defining some properties and assigning values to those properties and fetch those values and display on the next page.

So here is my class:

  1. public class NavigationClass  
  2. {  
  3.     public int Id  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Name  
  9.     {  
  10.         get;  
  11.         set;  
  12.          }  
  13.     public int Age  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  
Now I am assigning some values to these properties and navigating to the next page with these values.

In mainPage.xaml.cs on the button click event I assign values and navigate to the next page:

  1. private void Navigate_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    // Frame.Navigate(typeof(Page2),"Main Page");  
  4.     NavigationClass navCl1 = new NavigationClass();  
  5.     navCl1.Id = 12;  
  6.     navCl1.Name="Your name";  
  7.     navCl1.Age = 22;  
  8.     Frame.Navigate(typeof(Page2) , navCl1);  
  9.   
  10. }   


So when the navigate button is clicked the properties will assign some values and navigate to the next page. Now add a blank page to the project and put some controls on the page. Here I am using two controls, a button control and a TextBlock control. The TextBlock control will be used to display the value of the name property of the class and the button will be used to navigate as in the mainpage.

Page2.xaml

So here is the sample code:

  1. <Grid>  
  2.     <StackPanel>  
  3.         <TextBlock Name="Page2Textblock" Text="page2.xaml"/>  
  4.         <Button Name="navigation2" Content="Navigate"Click="navigation2_Click"/>  
  5.     </StackPanel>  
  6. </Grid>  


Now in the OnNavigated to method of the page2 I am using some code that will display the name in the textblock field on page2.

Page2.xaml.cs

  1. protected override void OnNavigatedTo(NavigationEventArgs e)  
  2.  {  
  3.      var nav = (NavigationClass)e.Parameter;  
  4.      Page2Textblock.Text = nav.Name;  
  5.  }   
And a button click event will be used to navigate to the third page.
  1. private void navigation2_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     Frame.Navigate(typeof(Page3));  
  4. }  
Now add another page to the project so that on a click it will navigate to the MainPage directly. So here is the code for page3.xaml.
  1. <Grid>  
  2.     <StackPanel>  
  3.         <TextBlock Name="textblockpage3" Text="Page3.xaml"/>  
  4.         <Button Name="goback" Content="Go Back"Click="goback_Click"/>      
  5.     </StackPanel>  
  6. </Grid>  
When page3 is loaded in the navigated to method of this page we will check the number of pages in the stack and then remove the number of pages by the BackStackDepth method and we will directly navigate to the mainpage rather than navigating page by page. It will display the number of pages in the stack as well.

Page3.xaml.cs

  1. protected override void OnNavigatedTo(NavigationEventArgs e)  
  2. {  
  3.     string myPages = "";  
  4.     foreach (PageStackEntry page in Frame.BackStack)  
  5.     {  
  6.        myPages += page.SourcePageType.ToString() + "\n";  
  7.     }  
  8.     textblockpage3.Text = myPages;  
  9.     Frame.BackStack.RemoveAt(Frame.BackStackDepth - 1);  
  10. }  
And when the go back button on page3 is clicked it will navigate to the mainpage.
  1. private void goback_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     Frame.GoBack();  
  4.      
  5. }  
                                                                                                                                                                                                                                         For more posts visit

Recommended Free Ebook
Next Recommended Readings