【问题标题】:Is there a global exception handler in Windows store apps?Windows 商店应用程序中是否有全局异常处理程序?
【发布时间】:2012-11-18 04:10:38
【问题描述】:

对于未处理的异常,至少,我希望能够捕获详细信息并将其写入文件,以供可能的后续“调试取证”。 Windows 商店应用程序中没有“OnTerminating”事件;有没有合适的地方/方法来完成这个?

更新

请参阅下面的评论。这是一个不适合下面的补充:

即使在删除 xaml sn-p 时,我仍然会收到那个错误消息,即使在清理和重建之后...??? 2-clicking err msg 将我带到 App.xaml 的顶部,现在的全部内容是:

<Application
    x:Class="SpaceOverlays.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SpaceOverlays">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

                <!-- 
                    Styles that define common aspects of the platform look and feel
                    Required by Visual Studio project and item templates
                 -->
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
    </Application.Resources>

更新 2

关闭 App.xaml 并重建后,一切都很好......???哦,好吧——我想一切都很好,结局很好。

更新 3

有趣的是,Windows Phone 应用 App.xaml.cs 默认有这个处理程序:

    // Global handler for uncaught exceptions.
    UnhandledException += Application_UnhandledException;

private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    if (Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        Debugger.Break();
    }
}

【问题讨论】:

    标签: c# debugging exception-handling windows-8 windows-store-apps


    【解决方案1】:

    对于 HTML5/JavaScript 应用程序,您可以将 onerror 事件作为捕获信息的最后机会。

    对于基于 XAML 的应用,您可以使用UnhandledException;但是,这仅捕获通过 XAML (UI) 框架出现的异常,即使在 InnerException 中,您也不总是能获得很多关于根本原因的信息。

    Windows 8.1 更新:UnhandledException 还将捕获由async void 方法创建的异常。在 Windows 8 中,此类异常只会使应用程序崩溃。 LunarFrog 在他们的网站上有一个很好的discussion

    【讨论】:

    • 我查看了“未处理的异常”链接。我在哪里添加那个 xaml sn-p?如果我把它放在 App.xaml 中,像这样: ...我得到编译时错误,“无法将内容添加到类型为'应用程序'的对象”更新添加到 diesbzg 之上。
    • 将代码中的异常添加到 App.xaml.cs 中的 App() 构造函数中:this.UnhandledException +=
    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 2014-09-06
    • 1970-01-01
    • 2012-06-05
    • 2010-12-13
    • 2019-07-26
    • 2014-04-17
    • 2012-12-31
    相关资源
    最近更新 更多