Every time MainPage navigates to SecondPage, it’s a different
instance of SecondPage. That’s why SecondPage always
starts out the same. It’s always a new instance.
If we want SecondPage to “remember” the last color it was set to,
something outside of SecondPage must be
responsible for saving that data. That could be MainPage.
Or, SecondPage could
save its state in isolated storage.
Isolated storage is much like regular disk storage. To access it, you
use classes in the System.IO.IsolatedStorage namespace. Every Windows Phone 7 application
has access to isolated storage but only to files that the application
itself has created. Isolated storage allows an application to save data between multiple
executions, and is ideal for saving
application settings.
A third solution is
provided by a class named PhoneApplicationService,
defined in the Microsoft.Phone.Shell
namespace. An instance of PhoneApplicationService
is created in the standard App.xaml file:
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>
Following the PhoneApplicationService tag are four events being associated with
handlers.
Don’t create a new PhoneApplicationService. You can obtain this existing PhoneApplicationService with the static PhoneApplicationService.Current property.
PhoneApplicationService contains a property named State,
which is a dictionary
that lets you save and restore data. This State property is of type IDictionary<string, object>. You store objects in this
dictionary using text keys. This data is only retained while the application is
running, so it’s not suitable for application settings that must be
preserved between multiple executions of a program. Data retained by the
applicaton only when it’s running is sometimes known as “transient” data.
Any object you store in this State dictionary must be serializable, that
is, it must be possible to convert the object into XML, and recreate the
object from XML. It must have a public parameterless constructor, and
all its public properties must either be serializable or be of types
that have Parse
methods to convert the strings back to objects.
It’s not always obvious
what objects are serializable and which ones are not. When I first
started experimenting, I tried to store SolidColorBrush
objects in the StateBrush has a property
named Transform of type Transform, an abstract class. I had to
serialize the Color instead. dictionary. The program raised an exception that said “Type
‘System.Windows.Media.Transform’ cannot be serialized.” It took awhile
to remember that
Let’s modify the previous program so that SecondPage uses this State property. In the SilverlightRetainData project,
everything is the same except for a usingMicrosoft.Phone.Shell namespace and two overrides in SecondPage. Here they are: directive for the
Example 1. Silverlight
Project: SilverlightRetainData File: SecondPage.xaml.cs (excerpt)
protected override void OnNavigatedFrom(NavigationEventArgs args) { if (ContentPanel.Background is SolidColorBrush) { Color clr = (ContentPanel.Background as SolidColorBrush).Color;
if (args.Content is MainPage) (args.Content as MainPage).ReturnedColor = clr;
// Save color PhoneApplicationService.Current.State["Color"] = clr; }
base.OnNavigatedFrom(args); }
protected override void OnNavigatedTo(NavigationEventArgs args) { // Retrieve color if (PhoneApplicationService.Current.State.ContainsKey("Color")) { Color clr = (Color)PhoneApplicationService.Current.State["Color"]; ContentPanel.Background = new SolidColorBrush(clr); }
base.OnNavigatedTo(args); }
|
During the OnNavigatedFrom
call, if there’s a valid Color object available, then it’s saved in the State dictionary with a key
of “Color”:
PhoneApplicationService.Current.State["Color"] = clr;
During the OnNavigatedTo override, if the key exists, then the Color value is loaded from the dictionary and
SolidColorBrush is made from the Color. The key
will not exist if you’ve just started running the program and you’ve
navigated to SecondPage for the first time. But on subsequent
navigations to SecondPage, the page is restored to the color you last set.
Every time you exit the
program by pressing the Back button on the main page, the State dictionary is
discarded with the rest of the PhoneApplicationService. This State dictionary is only suitable for saving transient
data that a
program needs to retain while it’s running. If you need to save data between multiple
executions of a program, use isolated storage.
Now try this: Navigate to SecondPage. Touch the screen to change the color. Now press
the phone’s hardware Start button. You’ve left the SilverlightRetainData
program. From the phone’s start screen, you can navigate to other
programs, but eventually you’ll want to press the phone’s Back button to
return to the SilverlightRetainData program and SecondPage. The color is still there.
Now go back to MainPage.
The color you set in SecondPage is
displayed. From MainPage, press the phone’s hardware Start button,
leaving the program. Navigate around a bit if you want but eventually
start pressing the Back button to come back to SilverlightRetainData and
MainPage.