- Saving persistent data with ApplicationData class
- LocalFolder for saving persistent data in files on the device
- LocalSettings for saving key-value pairs on the device
- RoamingFolder for saving persistent data files in the cloud; data is
accessible on different devices to same user
- RoamingSettings for saving key-value pairs in the cloud
- Other:
LocalCacheFolder
, TemporaryFolder
,
SharedLocalFolder
- OnSuspending() method in App.xaml.cs called when suspending
- Save the navigation state
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
Frame frame = Window.Current.Content as Frame;
ApplicationData.Current.LocalSettings.Values["NavigationState"] = frame.GetNavigationState();
deferral.Complete();
}
- Calling
GetNavigationState()
causes OnNavigatedFrom()
to be called when the app is terminated, which is ideal for saving the page's state
- Warning: Calls to
Frame.Navigate()
must either pass no
second parameter or a second parameter that is a primitive type like string, char, int, etc.
If the parameter is an object, an exception will be thrown because the object cannot be
serialized automatically by GetNavigationState()
.
- Restore navigation state in
OnLaunched()
so when app returns to same page
when brought back to Running state
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
// Skip...
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
var navigationState = ApplicationData.Current.LocalSettings.Values["NavigationState"] as string;
if (navigationState != null)
{
rootFrame.SetNavigationState(navigationState);
}
}
// Skip...
}
PreviousExecutionState
indicates what caused the app to
previously terminate: Terminated
or ClosedByUser
- Save persistent data in LocalSettings when navigating away from page (
OnNavigatedFrom()
is called when app is suspended)
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
ApplicationData.Current.LocalSettings.Values["username"] = username;
}
- Restore persistent data when navigating to the page in
OnNavigatedTo()
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (ApplicationData.Current.LocalSettings.Values.ContainsKey("username"))
{
string username = ApplicationData.Current.LocalSettings.Values["username"] as string;
}
}
- Multiple values can be stored in an ApplicationDataCompositeValue object
// In OnNavigatedFrom
var composite = new ApplicationDataCompositeValue();
composite["field1"] = textBox1.Text;
composite["field2"] = textBox2.Text;
ApplicationData.Current.LocalSettings.Values["pageState"] = composite;
// In OnNavigatedTo
var composite = ApplicationData.Current.LocalSettings.Values["pageState"] as ApplicationDataCompositeValue;
textBox1.Text = composite["field1"] as string;
textBox2.Text = composite["field2"] as string;
- To clear the state on a new navigation, check e.NavigationMode
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Other possible values: Back, Forward, Refresh
if (e.NavigationMode == NavigationMode.New)
{
// Disregard saved state
ApplicationData.Current.LocalSettings.Values.Remove("pageState");
}
else if (ApplicationData.Current.LocalSettings.Values.ContainsKey("pageState"))
{
// Restore saved state
var composite = ApplicationData.Current.LocalSettings.Values["pageState"] as ApplicationDataCompositeValue;
textBox1.Text = composite["field1"] as string;
textBox2.Text = composite["field2"] as string;
}
}