Saturday, February 16, 2013

Window Phone 8 App - Navigation

Today, I am going to explain how to do the navigation between pages and passing data between pages in Windows Phone 8 App. Before getting into application, please install following software's.

1. Visual Studio 2012
2. Windows Phone 8 SDK

Once you have installed the above software's, create the new project of Windows Phone 8 App from the project templates.

Navigation:
         To navigate from one page to another page(XAML), you have to use navigation service. See below code to navigate to Page1.xaml. If your file located in relative path, use Urikind as Relative.

     private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)  
     {  
       NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));  
     }  


Go Back:

     To navigate to previous page, you have to use GoBack() method in navigation service.


     private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)  
     {  
       NavigationService.GoBack();  
     }  


Passing Data between Pages:
         To pass data between pages either you can use QueryString method or Application class. Here I am explaining QueryString method.

Source (Passing as QueryString):
      

     private void Button_Click_1(object sender, RoutedEventArgs e)  
     {  
       NavigationService.Navigate(new Uri("/Page1.xaml?name=" + txtName.Text, UriKind.Relative));  
     }  


Destination (Override the OnNavigatedTo method in PhoneApplicationPage Class):

      protected override void OnNavigatedTo(NavigationEventArgs e)  
     {  
            base.OnNavigatedTo(e);  
        string name = string.Empty;  
        if (NavigationContext.QueryString.TryGetValue("name", out name))  
          txtBox1.Text = name;  
     }  



Screenshots:









No comments: