It is obvious that if your application redirects (navigates) from one page to another page, you will frequently want to pass values between these pages. Let's consider an example where a user needs to pass value of textbox from one page to another page, like when a user enters something in textbox of page2 and would like to see the 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.
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.
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 states that it is used to get the dictionary used for passing an application’s state between invocations.
Each and every Windows Phone application containing PhoneApplicationService which is 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)
PhoneApplicationSerivce States to pass information:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
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.
PhoneApplicationService States to retrieve information:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
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
PhoneApplicationService class plays a major role in the tombstoning game, since it exposes the
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 given code and debug the application, the emulator windows will launch up and run application as shown in the below pictures:
Demo of passing value between pages
Download source code - Try the source code to learn more.
Filed under: C#
, Windows Phone
Tagged: application state
, Windows Phone
, Windows Phone 8