Reputation: 3
I am trying to save objects with Prism when my UWP app is suspending so that they can be restored when it resumes or launches. The saving is being done when the Suspending event of the app fires and the object is being retrieved on Resume and LaunchApplicationAsync.
The object is restored correctly when I use the Visual Studio Suspend and Resume in debug but not when I do Suspend and Shutdown or close the app myself. The behaviour is the same for primitive properties with the RestorableState annotation.
When the app launches after a shutdown I can only see one item (key of "AppFrame" - looks like inserted by Prism) in the SessionState dictionary so it seems like the dictionary get reset. Is there anything special I need to do to persist the values I save beyond the Suspended state (i.e. when it is terminated or closed by user)?
Here is the launch method from App.xaml.cs:
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs e)
{
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
RootPageViewModel.ShellNavigation(typeof(SurveyListPage));
RootPageViewModel.RestorePropertyStates();
return Task.FromResult(true);
}
And the RestorePropertyStates method:
public void RestorePropertyStates()
{
if (SessionStateService.SessionState.ContainsKey(nameof(CurrentLocation)))
{
CurrentLocation = SessionStateService.SessionState[nameof(CurrentLocation)] as ViewLocation;
}
}
Also the method that saves the properties:
public void SavePropertyStates()
{
if (SessionStateService.SessionState.ContainsKey(nameof(CurrentLocation)))
{
SessionStateService.SessionState.Remove(nameof(CurrentLocation));
}
SessionStateService.SessionState.Add(nameof(CurrentLocation), CurrentLocation);
}
Upvotes: 0
Views: 372
Reputation: 39082
The reason is that ViewLocation
type is not a known type, so it cannot be serialized and stored. You have to add it to the list of known types using RegisterKnownType
method on SessionsStateService
or serialize it yourself to a simple type like string
.
I usually create a layer above the session state to serialize all complex types to JSON strings using Json.NET
library, which relieves the burden of having to remember to add new types as known :-) .
Upvotes: 0