Tombstoning and Task Switching - Day 3 - Part 8
- Posted: Nov 11, 2010 at 6:08 PM
- 23,154 Views
- 12 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
How does your application respond when a user clicks the Start button or Back button on Windows Phone 7? In this video, Bob explains the difference between the Launched, Activated, Closed, and Deactivated events, how your application can be notified via event handlers in the App.xaml.cs code-behind file, and how to take that opportunity to save the current state of the application. Then, once the user has re-launched the applications, the state information can be retrieved and the state of the application from the previous session can be restored. Bob also explains how a special feature of Isolated Storage called IsolatedStorageSettings can provide an easy way to save name / value pair information without having to create and access a text file.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
Does App.xaml.cs.SaveState() also need to use TryGetValue, since, the first time it is run, if we leave the page or hit start before we change the TextBox, there is nothing in the property bag and the access fails with an exception?
The sequence of Load, Activate, Deactivate, and Close calls was not obvious at first. And the fact that the debugger keeps dropping out makes it tough to follow. A useful extension to this module would be to record each of these events (WHEN it occurred) in the string we are saving.
These changes would go in App.xaml.cs:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
LoadState("L");
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
LoadState("A");
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
SaveState("D");
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
SaveState("C");
}
And then:
private void SaveState(string when)
{
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (phoneAppService.State.ContainsKey("MyValue"))
{
settings["MyValue"] = phoneAppService.State["MyValue"] + " " + when + " ";
}
else
{
settings["MyValue"] = when + " ";
}
}
private void LoadState(string when)
{
PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("MyValue"))
{
phoneAppService.State["MyValue"] = settings["MyValue"] + " " + when + " ";
}
else
{
phoneAppService.State["MyValue"] = when + " ";
}
}
David
12:12: It is unnecessary to condition 'TryGetValue' with 'ContainsKey' ('TryGetValue' combines 'ContainsKey' and 'Item' getter, see http://msdn.microsoft.com/en-us/library/bb347013(VS.95).aspx).
great video, thanks.
A very useful video but I have a question: I got the impression from the diagram on the link
http://msdn.microsoft.com/en-us/library/ff817008(v=VS.92).aspx">http://msdn.microsoft.com/en-us/library/ff817008(v=VS.92).aspx which is part of the instructions for submission that we are not "allowed" to run any code in the lauch event handler. This is the best place to run the LoadState() code as it is only done once. Are you able to clarify?
Dani.
Very good! very simple and useful. Thanks a lot!
I have tried this with the Simple Note application, which is the homework of day 3.
But whenever I press the start button, the app crashes and an error appears in the c# code.
I have tried the code of dwrogers, and it doesn't do anything at all.
So, is there any solution, as this is vital to me?.
Got this to work on my second attempt! One of the harder lessons so far.
I think I've done exactly as Bob instructed, but when I type something in the text box, tap the Windows key and then return to the application, the text has gone. Same with the Back key. Can't figure out what the difference could be.
Hi, here is the way how to have the testing device always set to Emulator.
http://www.pchenry.com/Home/tabid/36/EntryId/412/Tired-of-ALWAYS-having-to-change-to-the-WP7-emulator-in-Visual-Studio.aspx
I came across to the same problem of Vesku (May 23, 2011 at 5:22 AM) and I (not easily) solved it. Shortly, the line of code
if (phoneAppService.State.ContainsKey("MyValue"))
in the LoadState() method should be commented (or deleted). This solves the problem. I know the app in the video works but it can be easily a bug that has been corrected with the newest releases of Silverlight or whatsoever.
Remove this comment
Remove this thread
close