Windows商店应用程序中是否有全局异常处理程序?

17

对于未处理的异常,我想至少能够捕获详细信息并将其写入文件以供潜在的“调试取证”。在Windows Store应用程序中没有“OnTerminating”事件;是否有适当的位置/方式来完成此操作?

更新

请参见下面我的评论。以下是不适合下面的补充内容:

即使删除了XAML片段,我仍然会收到该错误消息,并且即使在清理和重建之后...??? 双击错误消息只会将我带到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();
    }
}
1个回答

8
对于HTML5/JavaScript应用程序,您有onerror事件作为捕获信息的最后机会。
对于基于XAML的应用程序,可以使用UnhandledException; 但是,它仅捕获通过XAML(UI)框架出现的异常,并且您并不总是能够获得有关根本原因的大量信息,即使在InnerException中也是如此。 Windows 8.1更新:UnhandledException还将捕获由async void方法创建的异常。 在Windows 8中,这种异常只会使应用程序崩溃。 LunarFrog在其网站上有一个很好的discussion

我查看了那个"未处理的异常"链接。我应该在哪里添加那个 XAML 代码片段?如果我将它放在 App.xaml 中,像这样:<Application UnhandledException="AppWideUnhandledExceptionsEventHandler"/> ...我会得到编译时错误,"无法向 'application' 类型的对象添加内容"。上面更新的内容添加了。 - B. Clay Shannon-B. Crow Raven
2
在 App.xaml.cs 文件的 App() 构造函数中添加异常处理代码:this.UnhandledException += <按两次 Tab 键生成处理程序>。 - Jim O'Neil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接