设为首页 加入收藏

TOP

【转】UWP 捕获全局异常(二)
2017-10-11 14:35:21 】 浏览:1197
Tags:UWP 捕获 全局 异常
gger.Break();
#endif return exWrapper.Handled; } public event EventHandler<AysncUnhandledExceptionEventArgs> UnhandledException; } public class AysncUnhandledExceptionEventArgs : EventArgs { public bool Handled { get; set; } public Exception Exception { get; set; } } }

使用实例:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;

    this.UnhandledException += App_UnhandledException;

}

private void RegisterExceptionHandlingSynchronizationContext()
{
    ExceptionHandlingSynchronizationContext
        .Register()
        .UnhandledException += SynchronizationContext_UnhandledException;
}

private async void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
{
    e.Handled = true;

    await new MessageDialog("Application Unhandled Exception:\r\n" + e.Exception.Message)
        .ShowAsync();
}

private async void SynchronizationContext_UnhandledException(object sender, AysncUnhandledExceptionEventArgs e)
{
    e.Handled = true;

    await new MessageDialog("Synchronization Context Unhandled Exception:\r\n" + e.Exception.Message)
        .ShowAsync();
}

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    RegisterExceptionHandlingSynchronizationContext();

#if DEBUG
    if (System.Diagnostics.Debugger.IsAttached)
    {
        this.DebugSettings.EnableFrameRateCounter = true;
    }
#endif

    Frame rootFrame = Window.Current.Content as Frame;

    // 不要在窗口已包含内容时重复应用程序初始化,
    // 只需确保窗口处于活动状态
    if (rootFrame == null)
    {
        // 创建要充当导航上下文的框架,并导航到第一页
        rootFrame = new Frame();

        rootFrame.NavigationFailed += OnNavigationFailed;

        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: 从之前挂起的应用程序加载状态
        }

        // 将框架放在当前窗口中
        Window.Current.Content = rootFrame;
    }

    if (rootFrame.Content == null)
    {
        // 当导航堆栈尚未还原时,导航到第一页,
        // 并通过将所需信息作为导航参数传入来配置
        // 参数
        rootFrame.Navigate(typeof(MainPage), e.Arguments);
    }
    // 确保当前窗口处于活动状态
    Window.Current.Activate();
}

protected override void OnActivated(IActivatedEventArgs args)
{
    RegisterExceptionHandlingSynchronizationContext();
    base.OnActivated(args);
}

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[UWP] 自定义一个ItemsPanel 下一篇音乐播放器页面之音频播放页面设计

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目