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.
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.
Now write the following code to pass data from MainPage.xaml to Page1.xaml.
C# Code
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- string Name = "C sharp Corner";
- string Technology = "Windows Phone";
- string uri = string.Format("/Page1.xaml?name={0}&&technology={1}", Name, Technology);
- 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.
Write the following code to get data from MainPage.
C# Code
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- string name = NavigationContext.QueryString["name"];
- string technology = NavigationContext.QueryString["technology"];
- 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.