How to manage Page State in Windows Phone

No.of Views1625
Bookmarked0 times
Downloads 
Votes0
By  usamawahabkhan   On  05 Mar 2011 05:03:40
Tag : Windows Phone , Applications
In this article i will explain how to manage page state in windows phone.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article i will explain how to manage page state in windows phone.There are a few practical elements to state management, so let’s devote a few of these mini-tutorials to the exact mechanisms involvedTo start, let’s examine the scenario where the user is on a first page, fills in some data, and then navigates to a second page. For whatever reason, the user clicks the back button on the phone and is returned to the first page. At this point the user expects to see the data already filled in to be there, ready for editing.

If the user hits the back button and the fields already created are now empty the application is in a state technically referred to as “sucks.”

To prevent this, you will want to store the values in a State dictionary and restore those values if the user hits the back button. Fortunately, this is fairly easy to do, though you have to be a bit careful when restoring. To see this, create a new application with two pages: Mainpage.xaml and Page2.xaml. On MainPage.xaml create a few text input fields and a button, on page 2 put anything you like. You can see the MainPage.xaml I created in the illustration above.

 

Image Loading

Saving and Restoring State

Saving the state is pretty straightforward.

As noted in the earlier article, when you are navigating away form the page the OnNavigatedFrom event is raised. Each page has an individual pre-defined State dictionary, to which you can save the state of your controls

C# Code

protected override void OnNavigatedFrom(
System.Windows.Navigation.NavigationEventArgs e)
{
State["Name"] = FullName.Text;
State["eMail"] = eMail.Text;
State["Phone"] = Phone.Text;
base.OnNavigatedFrom(e);
}

Restoring is just a bit trickier as you have to make sure that the value you attempt to retrieve from the dictionary actually exists. For this, I like to use the TryGetValue method, as it does not throw an exception. It takes two values and returns a boolean. The first parameter is the name of the “key” you are looking for. The second is an out parameter that will hold the value if found. The returned value is a boolean, true if the key was found, false if it was not.

Remembering that the dictionary holds objects, we search for the key and if it is found we call ToString on the values obtained before writing them back to the control,

Here is the complete code behind page,

using System;
using System.Windows;
using Microsoft.Phone.Controls;
namespace PageState
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
GoToPage2.Click += GoToPage2_Click;
}
void GoToPage2_Click( object sender, RoutedEventArgs e )
{
NavigationService.Navigate(
new Uri( "/Page2.xaml", UriKind.Relative ) );
}
protected override void OnNavigatedFrom(
System.Windows.Navigation.NavigationEventArgs e )
{
State["Name"] = FullName.Text;
State["eMail"] = eMail.Text;
State["Phone"] = Phone.Text;
base.OnNavigatedFrom( e );
}
protected override void OnNavigatedTo(
System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedTo( e );
object val;
if ( State.TryGetValue( "Name", out val ) )
FullName.Text = val.ToString();
if ( State.TryGetValue( "eMail", out val ) )
eMail.Text = val.ToString();
if ( State.TryGetValue( "Phone", out val ) )
Phone.Text = val.ToString();
}
}
}

Hopes help and thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
usamawahabkhan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Pakistan
Joined date-06 May 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</