问题
I have an app with 2 pages calendar.xaml(start page) and historystatistics.xaml. Error is thrown when I navigate to historystatistics and back to calendar and then try to terminate the app by the pulling down gesture. The mentioned error occurs at the SaveAsync function in OnSuspending of App.xaml.
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
CycleManager cycMan = CycleManager.Instance;
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
if (cycMan.Reset != true)
{
await Appname.Common.SuspensionManager.SaveAsync(); // **ERROR**
}
deferral.Complete();
}
It says
An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code.
GetNavigationState doesn't support serialization of a parameter type which was passed to Frame.Navigate.
Where did I wrong? I did a simple navigation from historystatistics page from xaml like this
<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
回答1:
The problem is that SuspensionManager uses Frame.GetNavigationState() to get the history of the Frame. It then tries to serialize the navigation history to a string, unfortunately it has no way to know how to serialize custom complex types .
From MSDN :
Note The serialization format used by these methods is for internal use only. Your app should not form any dependencies on it. Additionally, this format supports serialization only for basic types like string, char, numeric and GUID types.
The best solution for this problem to rewrite the NavigationParameter or if you didn't want to save your app state just comment the line :
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//await SuspensionManager.SaveAsync();
deferral.Complete();
}
来源:https://stackoverflow.com/questions/16735061/system-runtime-interopservices-comexception-caused-while-terminating-the-app