【发布时间】:2017-09-12 20:52:45
【问题描述】:
我正在尝试在我的 UWP 应用程序暂停时使用 Prism 保存对象,以便在它恢复或启动时可以恢复它们。当应用程序的 Suspending 事件触发并且在 Resume 和 LaunchApplicationAsync 上检索对象时,正在完成保存。
当我在调试中使用 Visual Studio Suspend 和 Resume 时,对象会正确恢复,但在我自己执行 Suspend 和 Shutdown 或关闭应用程序时不会。带有 RestorableState 注释的原始属性的行为是相同的。
当应用程序在关闭后启动时,我只能在 SessionState 字典中看到一个项目(“AppFrame”的键 - 看起来像是由 Prism 插入的),所以看起来字典被重置了。我需要做些什么来将我保存的值保持在 Suspended 状态之外(即当它被用户终止或关闭时)?
这是来自 App.xaml.cs 的启动方法:
protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs e)
{
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
RootPageViewModel.ShellNavigation(typeof(SurveyListPage));
RootPageViewModel.RestorePropertyStates();
return Task.FromResult(true);
}
还有 RestorePropertyStates 方法:
public void RestorePropertyStates()
{
if (SessionStateService.SessionState.ContainsKey(nameof(CurrentLocation)))
{
CurrentLocation = SessionStateService.SessionState[nameof(CurrentLocation)] as ViewLocation;
}
}
还有保存属性的方法:
public void SavePropertyStates()
{
if (SessionStateService.SessionState.ContainsKey(nameof(CurrentLocation)))
{
SessionStateService.SessionState.Remove(nameof(CurrentLocation));
}
SessionStateService.SessionState.Add(nameof(CurrentLocation), CurrentLocation);
}
【问题讨论】: