gpt4 book ai didi

c# - 终止应用程序时导致的 System.Runtime.InteropServices.COMException

转载 作者:太空宇宙 更新时间:2023-11-03 16:13:15 25 4
gpt4 key购买 nike

我有一个包含 2 页 calendar.xaml(起始页)和 historystatistics.xaml 的应用程序。当我导航到 historystatistics 并返回到日历然后尝试通过下拉手势终止应用程序时会抛出错误。上述错误发生在App.xaml的OnSuspending中的SaveAsync函数处。

    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();
}

它说

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.

我哪里做错了?我像这样从 xaml 的 historystatistics 页面做了一个简单的导航

<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>

最佳答案

问题在于 SuspensionManager 使用 Frame.GetNavigationState() 来获取 Frame 的历史记录。然后它尝试将导航历史序列化为字符串,不幸的是它无法知道如何序列化自定义复杂类型。

来自 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.

此问题的最佳解决方案是重写 NavigationParameter,或者如果您不想保存您的应用程序状态,只需注释该行:

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//await SuspensionManager.SaveAsync();
deferral.Complete();
}

关于c# - 终止应用程序时导致的 System.Runtime.InteropServices.COMException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16735061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com