How to pass values between pages in Windows Phone 8 using PhoneApplicationService
It is obvious that if your application redirects (navigate) from one page to another page, you will frequently want to pass values between these pages. Let’s consider an example where a user need to pass a value of a textbox from one page to another page, like when a user enters something in the textbox of page2 and would like to see same entered(from page2) value in the 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 the 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.
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 the 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)
txtvalue.Text = (string)PhoneApplicationService.Current.State["Text"];
to navigate next page
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 to give the code and debug the application, the emulator windows will launch up and run the application as shown in below pics –
Download Source Code
Try source code to Learn more. Click Me!