简体   繁体   English

在 WinUI 3 中关闭 WindowEx 窗口时出现 InvalidOperationException

[英]InvalidOperationException when closing a WindowEx window in WinUI 3

Recently, I created a new WinUI 3 (v1.2) desktop app using Template Studio for WinUI .最近,我使用Template Studio for WinUI创建了一个新的WinUI 3 (v1.2) 桌面应用程序。 Because starting unpackaged apps in Debug is so much faster than packaged apps, I chose an unpackaged app in the setup wizard.因为在Debug中启动未打包的应用程序比打包的应用程序快得多,所以我在设置向导中选择了一个未打包的应用程序。 The resulting barebones code created an app that always crashed when closed using the Close method or the Close button on the System Menu .生成的准系统代码创建了一个应用程序,该应用程序在使用Close方法或System Menu上的Close按钮关闭时总是崩溃。

The app throws an unhandled exception which VS 2022 catches in App.gics in the following section:该应用程序抛出一个未处理的异常, VS 2022在以下部分的App.gics中捕获该异常:

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
    UnhandledException += (sender, e) =>
    {
        if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
    };
#endif

You can disable this by setting the DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION variable in the project Properties (under Build -> Conditional compilation symbols ) but the program will still throw the error (this code simply gives you a convenient way to trap unexpected errors).您可以通过在项目Properties (在Build -> Conditional compilation symbols下)中设置DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION变量来禁用此功能,但程序仍会抛出错误(此代码只是为您提供了一种捕获意外错误的便捷方法)。

Can anyone explain why this is happening with an unmodified new project and how to correct it, please?任何人都可以解释为什么未修改的新项目会发生这种情况以及如何纠正它吗?

The template code generates a MainWindow that is of the class WindowEx .模板代码生成一个属于WindowEx MainWindow This nifty extension of the Window class adds quite a few helpful features (see: WinUIEx Wiki ) but some of them aren't completely documented yet. Window类的这个漂亮扩展添加了很多有用的功能(请参阅: WinUIEx Wiki ),但其中一些功能尚未完全记录。 The WindowEx class creates a WinUIEx.WindowManager to manage the WindowEx window. WindowEx类创建一个WinUIEx.WindowManager来管理WindowEx窗口。 When the WindowEx is closed, WinUIEx.WindowManager.Window_Closed() is called which then calls WinUIEx.WindowManager.SavePersistence() which is the cause of the crash mentioned above.WindowEx关闭时,调用 WinUIEx.WindowManager.Window_Closed( WinUIEx.WindowManager.Window_Closed() ,然后调用WinUIEx.WindowManager.SavePersistence() ,这是上述崩溃的原因。

SavePersistence() tries to save some of the window characteristics so they can be restored when the app restarts. SavePersistence()尝试保存一些窗口特征,以便在应用程序重新启动时可以恢复它们。 However, the persistence services are only enabled for packaged apps , not unpackaged (which I used).但是,持久性服务仅针对打包的应用程序启用,而不是未打包的(我使用过)。 To turn this behavior off, set the PersistenceId property of the WinUIEx.WindowManager instance to either null , string.Empty , or "" (the template sets PersistenceId = "MainWindow" by default).要关闭此行为,请将WinUIEx.WindowManager实例的PersistenceId属性设置为nullstring.Empty"" (模板默认设置PersistenceId = "MainWindow" )。 Just add只需添加

    var manager = WinUIEx.WindowManager.Get(MainWindow);
    manager.PersistenceId = string.Empty;

somewhere after MainWindow has been instantiated (I put it in App.OnLaunched ).MainWindow被实例化之后的某个地方(我把它放在App.OnLaunched中)。 I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM