我試圖在UWP應用程序掛起時使用Prism保存對象,以便在恢復或啓動時恢復它們。當應用程序的掛起事件觸發並且在Resume和LaunchApplicationAsync上檢索對象時,正在進行保存。UWP Prism SessionStateService在用戶關閉後丟失狀態
當我在調試中使用Visual Studio Suspend和Resume時,對象被正確恢復,但是當我自己執行Suspend and 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);
}
我已將ViewLocation添加到已知類型。這是一個複雜的類型,但我認爲它應該串行化原始屬性,忽略未知的類型中定義的對象?即使這種情況已被破壞(因爲ViewLocation上的屬性不能被序列化)應該使用[RestorableState]保存原語不起作用? – fralama