Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

How to Pass Values Between Pages in Windows Phone 8 using PhoneApplicationService

, 11 Feb 2014
Rate this:
Please Sign up or sign in to vote.
How to Pass Values Between Pages in Windows Phone 8 using PhoneApplicationService

Introduction

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.

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 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:

PhoneApplicationService.Current.State 

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
            base.OnNavigatedFrom(e);
            // 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:

 NavigationService.GoBack();

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

To use PhoneApplicationService States to retrieve information:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(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 given code and debug the application, the emulator windows will launch up and run application as shown in the below pictures:

rrk-passvalue1

Demo of passing value between pages

Download source code - Try the source code to learn more.


Filed under: C#, CodeProject, Windows Phone Tagged: application state, C#, Windows Phone, Windows Phone 8, WP8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RaviRanjankr
Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

 
Questionnice PinprofessionalBillW3311-Mar-14 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 11 Feb 2014
Article Copyright 2014 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid