c# – 使用Xamarin Android时的例外情况

前端之家收集整理的这篇文章主要介绍了c# – 使用Xamarin Android时的例外情况前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近开始使用Xamarin Android学习移动开发.我正在使用VS 2012.我注意到当我打开一个android项目时,Debug下的所有异常 – >例外情况未经检查.我认为这就是为什么代码中抛出的异常不会以我习惯于桌面开发的方式显示的原因.当我在Debug-> Exceptions窗口中检查异常并尝试将解决方案部署到模拟器时失败 – 没有错误,但应用程序无法在模拟器上启动.

所以我的问题是:使用VS 2012或VS 2010和模拟器开发Android时,这是正常的行为吗?有没有办法以“正常方式”查看抛出的异常,而不仅仅是在输出窗口中.如果我使用实际的Android设备进行调试,会改变吗?

解决方法

我的答案是否定的

为什么?

There is one important thing you have to understand about the nature
of an Unhandled exception in Android,there isn’t one…. in Android
it’s an Uncaught exception which means you can’t “handle” it or
recover from it like you maybe would in a .Net environment.

Xamarin(Mono) internally “handles” those uncaught exceptions by
surrounding literally everything with try-catch and raising the
Unhandled event but that is besides the point. It is also discouraged
to interact with the UI as well for varIoUs reasons.

Theoretically there are several “workarounds” for displaying a dialog
to the user or restarting the app,none of which I’d recommend on
doing. Instead you should surround sensitive areas with try-catch
clauses to handle expected exceptions,as for the unexpected one’s
just use an exception reporting component and update your app after
analyzing the reported exceptions

解释From

你也可以从应用程序中捕获未加扰的异常

创建名称ErrorActivity等基本活动

看看这个例子

protected override void OnCreate(Bundle bundle)
 {
        //register error handlers
                    AppDomain.CurrentDomain.UnhandledException += ErrorHandler.CurrentDomainOnUnhandledException;
                    TaskScheduler.UnobservedTaskException += ErrorHandler.TaskSchedulerOnUnobservedTaskException;
  }

错误处理程序类中

public static class ErrorHandler
    {
        /// <summary>
        ///     Tasks the scheduler on unobserved task exception.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="unobservedTaskExceptionEventArgs">
        ///     The <see cref="UnobservedTaskExceptionEventArgs" /> instance containing
        ///     the event data.
        /// </param>
        public static void TaskSchedulerOnUnobservedTaskException(object sender,UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
        {
            var newExc = new Exception("TaskSchedulerOnUnobservedTaskException",unobservedTaskExceptionEventArgs.Exception);
            LogUnhandledException(newExc);
        }

        /// <summary>
        ///     Currents the domain on unhandled exception.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="unhandledExceptionEventArgs">
        ///     The <see cref="UnhandledExceptionEventArgs" /> instance containing the event
        ///     data.
        /// </param>
        public static void CurrentDomainOnUnhandledException(object sender,UnhandledExceptionEventArgs unhandledExceptionEventArgs)
        {
            var newExc = new Exception("CurrentDomainOnUnhandledException",unhandledExceptionEventArgs.ExceptionObject as Exception);
            LogUnhandledException(newExc);
        }

        /// <summary>
        ///     Logs the unhandled exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        internal static void LogUnhandledException(Exception exception)
        {
            try
            {
                string error =
                    $"Exception Caught:{DateTime.Now:F} The Error Message IS {exception.Message}\n\r full stack trace is {exception.ToString()} ";
#if DEBUG
                const string errorFileName = "errorlog.txt";
                var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                // iOS: Environment.SpecialFolder.Resources
                var errorFilePath = System.IO.Path.Combine(libraryPath,errorFileName);
                System.IO.File.WriteAllText(errorFilePath,error);
                Android.Util.Log.Error("Crash Report error not handled",ex.ToString());
    #else
                    // Log to Android Device Logging.
                    Android.Util.Log.Error("Crash Report",error);
    #endif
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Error("Crash Report error not handled",ex.ToString());
                    // just suppress any error logging exceptions
                }
            }
        }

现在你可以像这样继承ErrorActivity的所有活动

Public class Fooactivity:ErrorActivity
{
}

现在你可以在应用程序中处理错误了.所以你从日志文件..或Android设备日志监视器获取错误日志.希望这有助于……

原文链接:https://www.f2er.com/csharp/243251.html

猜你在找的C#相关文章