How to pass values between pages in Windows Phone 8 using PhoneApplicationService

It is obvious that if your application redirect(navigate) from one page to another page, you will frequently want to pass values between these pages. Lets consider a example where a user need to pass value of textbox from one page to another page, like when a user enter something in textbox of page2 and would like to see same entered(from page2) value in textbox of page1. We can pass information between pages in various ways but here in this article we are going to use PhoneApplicationService to pass values between pages in Windows Phone 8.

Using PhoneApplicationService to accomplish task of passing values between pages is not a cumbersome task. We should know what is PhoneApplicationService before learning what is the actual use of PhoneApplicationService to pass values between pages.

PhoneApplicationService –

The PhoneApplicationService is a class of Microsoft.Phone.Shell Namespace. The PhoneApplicationService class provides access to various conditions of the application’s lifetime. This includes management of the application’s idle behavior and management of the application’s state when it becomes active or inactive.
Read More : MSDN-PhoneApplicationService Class

Using PhoneApplicationSerivce States –

PhoneApplicationService.State Property state that it is used to Gets the dictionary used for passing an application’s state between invocations.

Each and every Windows Phone application containing PhoneApplicationService which defined in Microsoft.Phone.Shell. Every application has its one PhoneApplicationService defined in its App.xaml file of a Windows Phone project. We can use this service’s property called “State”. These “States” are all transient data which means they are available only for one instant of your application which means once you restart your application these states will be lost. Also we should not try to create a new PhoneApplicationService instead we must try to use the service from the current instant. Which can be retrieved using this code:


Note – Windows Phone applications are deactivated when the user navigates to another application. When the user returns to the application, by using the Back button or by completing a Launcher or Chooser task, the application is reactivated. An application can store transient application state in the State dictionary in the handler for the Deactivated event. In the Activated event handler, an application can use the values stored in the State dictionary to transient application state. (Reference MSDN)

To Use PhoneApplicationSerivce States to pass information –

 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
            // Called when a page becomes the active page in a frame
            // Text is param, you can define anything instead of Text but remember you need to further use same param.
            PhoneApplicationService.Current.State["Text"] = txtboxvalue.Text;

to go back to first page


Note – Page.OnNavigatedFrom method Invoked immediately after the Page is unloaded and is no longer the current source of a parent Frame.

To Use PhoneApplicationSerivce States to retrieve information –

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            if (PhoneApplicationService.Current.State.ContainsKey("Text"))
                txtvalue.Text = (string)PhoneApplicationService.Current.State["Text"];


to navigate next page

  NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));

Note – Page.OnNavigatedTo method Invoked when the Page is loaded and becomes the current source of a parent Frame.

How is PhoneApplicationService.Current.State remembered
The PhoneApplicationService class plays a major role in the tombstoning game, since it exposes the Launching, Deactivate, Activated, and Closing events that have the corresponding methods (which you already saw) in the App.xaml.cs file. The objects are serialized (using DataContractSerializer) and then kept in memory. This is different (and faster) compared to IsolatedStorage which saves the data to disk.

When you use give code and debug the application, the emulator windows will launch up and run application as shown in below pics –


Demo of passing value between pages

Download Source Code
Try source code to Learn more. Click Me!


About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in C#, CodeProject, Windows Phone and tagged , , , , . Bookmark the permalink.

2 Responses to How to pass values between pages in Windows Phone 8 using PhoneApplicationService

  1. Hanna B says:

    Thanks for tips, I want to pass multiple data between pages. sir please tell me how to do it ?

  2. you need to make number of keys as many you want to pass between pages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s