How to pass data between Different Pages in Windows Phone application

In this article we will explore different methods of passing data between different pages in Windows phone application.

There are different ways of passing data between pages as given below

1. QueryString

2. Public property in App.xaml

3. Global variables

 

Let us assume that we have got four pages in our Application having the name MainPage, Page1, Page2, Page3.

Now we shall be passing data from MainPage to these three pages by three different method as given above.

Follow the different steps given below

 

1. Create a new project called Datapassing.

 


2. First I shall show you how to pass data between pages using QueryString.

 

Using QueryString

Create a button on the MainPage and add the following code given below in the click event handler of the button to Navigate to Page1.


 

In the code given above we have passed two variables name and rollno in QueryString.

Now go to Page1.xaml and add the following code given below in the Pageloaded Event handler of the Page1.xaml.

 

In the following code given above we read the two variables passed by QueryString using their key values (name and rollno).

 


 

3. We can use public property in App.xaml.cs to pass data between Pages.

 

Using Public property in App.xaml.cs

First go to App.xaml.cs and add the following lines of code given below above the constructor of the page.


 

It basically acts as declaring a static global variable in App.xaml.cs and using it from any part of the application.

 


 

Now create a button on the MainPage and add the following code given below in the click event handler of the button to Navigate to Page2.

 

In the above code we set the values of the public app variable name and rollno so that they can be easily be read in another page.

Now go to Page2.xaml and add the following code given below in the Pageloaded Event handler of the Page2.xaml.


 

In the following code given above we read the values of the public properties of the App.

 


 

4. The third method Global variable is quite similar to the Second one.

 

Using Global Variables

To use global variables in the application we need to first add a global class in the application.

We can simply achieve this by adding a new item->class having the name Globalclass into the project.

 


 


 

Now simply add the following code given below into the class.

 

In the following code given above we have initialized the variables to empty string and zero so that their values canbe changes in other pages.

 


 

Now create a button on the MainPage and add the following code given below in the click event handler of the button to Navigate to Page3.


 

Now go to Page3.xaml and add the following code given below in the Pageloaded Event handler of the Page3.xaml.

 

Now run the following project on emulator. You will observe the output as shown below.

 


 


 

I hope this post will be helpful to you. If you have any doubts related to this post you can download the full source project file from the link given below.

Download Full project file Datapassing.zip