How to Pass Data Among Pages in Windows Phone Application

In this article we will see how to pass data among various pages in a Windows Phone application.

There are various ways to pass data among pages. Here I am using the Query String method.

Create a new Windows Phone 8 application with a proper name and add one new page as shown in the following screen.

Windows phone protect page

Now we shall be passing data from MainPage to Page1 using a Query String.

Create a button on the MainPage to pass data from MainPage to Page1 as shown in the following.

enter page name

Now write the following code to pass data from MainPage.xaml to Page1.xaml.

C# Code

  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.       string Name = "C sharp Corner";  
  4.       string Technology = "Windows Phone";  
  5.       string uri = string.Format("/Page1.xaml?name={0}&&technology={1}", Name, Technology);  
  6.       NavigationService.Navigate(new Uri(uri, UriKind.Relative));  
In this demo we have passed the two variables name and technology.

Now go to Page1.xaml and add one button to get the data from MainPage as shown in the following.

xaml page

Write the following code to get data from MainPage.

C# Code
  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.       string name = NavigationContext.QueryString["name"];  
  4.       string technology = NavigationContext.QueryString["technology"];  
  5.       MessageBox.Show("Name:" + name +" | "+"Technology:" + technology);  
Run the application by pressing F5 key directly in your keyboard. Click the “Go to Page1” button and “Get Data” button as in the following screen.

goto page1 and get data

                                          *Windows phone page

Up Next
    Ebook Download
    View all
    Learn
    View all