ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> Windows Phone开发 >> UWP 捕获全局异常

UWP 捕获全局异常

来源:网络整理     时间:2017-01-14     关键词:

本篇文章主要介绍了" UWP 捕获全局异常",主要涉及到方面的内容,对于Windows Phone开发感兴趣的同学可以参考一下: 转自:http://www.cnblogs.com/youngytj/p/4749004.html异步(async)方法中的异常无法被App的Unhandled...

转自:http://www.cnblogs.com/youngytj/p/4749004.html

异步(async)方法中的异常无法被App的UnhandledException捕获的问题

这是一个比较严重的问题.目前已知很多的做法就是局部try catch来解决这个问题.这样做是很容易导致Process被强制终止然后引起闪退的问题的.

我这里用了一个线程同步模型类解决这个问题.

using System;
using System.Threading;
using Windows.UI.Xaml.Controls;

namespace AiJianShu.ExceptionHandler
{
    internalclass ExceptionHandlingSynchronizationContext : SynchronizationContext
    {
        ///<summary>/// 注册事件.  需要在OnLaunched和OnActivated事件中调用
        ///</summary>///<returns></returns>publicstatic ExceptionHandlingSynchronizationContext Register()
        {
            var syncContext = Current;
            if (syncContext == null)
                thrownew InvalidOperationException("Ensure a synchronization context exists before calling this method.");


            var customSynchronizationContext = syncContext as ExceptionHandlingSynchronizationContext;


            if (customSynchronizationContext == null)
            {
                customSynchronizationContext = new ExceptionHandlingSynchronizationContext(syncContext);
                SetSynchronizationContext(customSynchronizationContext);
            }


            return customSynchronizationContext;
        }

        ///<summary>/// 将线程的上下文绑定到特定的Frame上面
        ///</summary>///<param name="rootFrame"></param>///<returns></returns>publicstatic ExceptionHandlingSynchronizationContext RegisterForFrame(Frame rootFrame)
        {
            if (rootFrame == null)
                thrownew ArgumentNullException("rootFrame");

            var synchronizationContext = Register();

            rootFrame.Navigating += (sender, args) => EnsureContext(synchronizationContext);
            rootFrame.Loaded += (sender, args) => EnsureContext(synchronizationContext);

            return synchronizationContext;
        }

        privatestaticvoid EnsureContext(SynchronizationContext context)
        {
            if (Current != context)
                SetSynchronizationContext(context);
        }


        privatereadonly SynchronizationContext _syncContext;


        public ExceptionHandlingSynchronizationContext(SynchronizationContext syncContext)
        {
            _syncContext = syncContext;
        }


        publicoverride SynchronizationContext CreateCopy()
        {
            returnnew ExceptionHandlingSynchronizationContext(_syncContext.CreateCopy());
        }


        publicoverridevoid OperationCompleted()
        {
            _syncContext.OperationCompleted();
        }


        publicoverridevoid OperationStarted()
        {
            _syncContext.OperationStarted();
        }


        publicoverridevoid Post(SendOrPostCallback d, object state)
        {
            _syncContext.Post(WrapCallback(d), state);
        }


        publicoverridevoid Send(SendOrPostCallback d, object state)
        {
            _syncContext.Send(d, state);
        }


        private SendOrPostCallback WrapCallback(SendOrPostCallback sendOrPostCallback)
        {
            return state =>            {
                try                {
                    sendOrPostCallback(state);
                }
                catch (Exception ex)
                {
                    if (!HandleException(ex))
                        throw;
                }
            };
        }

        privatebool HandleException(Exception exception)
        {
            if (UnhandledException == null)
                returnfalse;

            var exWrapper = new AysncUnhandledExceptionEventArgs
            {
                Exception = exception
            };

            UnhandledException(this, exWrapper);

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
#endifreturn exWrapper.Handled;
        }

        publicevent EventHandler<AysncUnhandledExceptionEventArgs> UnhandledException;
    }

    publicclass AysncUnhandledExceptionEventArgs : EventArgs
    {
        publicbool Handled { get; set; }
        public Exception Exception { get; set; }
    }
}

使用实例:

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

    this.UnhandledException += App_UnhandledException;

}

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

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

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

privateasyncvoid SynchronizationContext_UnhandledException(object sender, AysncUnhandledExceptionEventArgs e)
{
    e.Handled = true;

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

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

protectedoverridevoid OnActivated(IActivatedEventArgs args)
{
    RegisterExceptionHandlingSynchronizationContext();
    base.OnActivated(args);
}

以上就介绍了 UWP 捕获全局异常,包括了方面的内容,希望对Windows Phone开发有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_4195865.html

相关图片

相关文章