xamarin - 找出应用程序在启动时崩溃的原因的最佳方法是什么?

标签 xamarin xamarin.forms xamarin.ios crash

我现在遇到了问题:当我开始调试时,应用程序直接崩溃并且调试过程停止。我看不到任何日志或错误消息。我不知道在这种情况下我该怎么办?

我试图将 -v -v -v -v 添加到附加的 mtouch 参数中。但在应用程序刚刚停止调试时没有看到任何打印出来。

有没有办法解决这样的问题?

最好的祝福

最佳答案

您应该尝试像这样捕获未处理的异常

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
...


#region Error handling

        private void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", e.Exception);
            LogUnhandledException(newExc);
        }

        private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var newExc = new Exception("CurrentDomainOnUnhandledException", e.ExceptionObject as Exception);
            LogUnhandledException(newExc);
        }

        internal static void LogUnhandledException(Exception exception)
        {
            try
            {
                const string errorFileName = "Fatal.log";
                var libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources); // iOS: Environment.SpecialFolder.Resources
                var errorFilePath = Path.Combine(libraryPath, errorFileName);
                var errorMessage = string.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
                DateTime.Now, exception.ToString());
                File.WriteAllText(errorFilePath, errorMessage);
            }
            catch
            {
                // just suppress any error logging exceptions
            }
        }

        // If there is an unhandled exception, the exception information is diplayed 
        // on screen the next time the app is started (only in debug configuration)
        [Conditional("DEBUG")]
        private static void DisplayCrashReport()
        {
            const string errorFilename = "Fatal.log";
            var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
            var errorFilePath = Path.Combine(libraryPath, errorFilename);

            if (!File.Exists(errorFilePath))
            {
                return;
            }

            var errorText = File.ReadAllText(errorFilePath);

            var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear") { UserInteractionEnabled = true };
            alertView.Clicked += (sender, args) =>
            {
                if (args.ButtonIndex != 0)
                {
                    File.Delete(errorFilePath);
                }
            };
            alertView.Show();
        }

        #endregion

关于xamarin - 找出应用程序在启动时崩溃的原因的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60529694/

相关文章:

android - ViewPagerIndicator 导致随机崩溃

xamarin - 找不到 API Level 的 android.jar 。沙马林

ios - iOS 中的推送通知服务扩展未执行

ios - 如何确定哪些框架触发了对隐私敏感的.plist问题

xamarin - 如何显示远程iOS模拟器日志?

c# - 在 Xamarin Forms 中渲染 HTML 并从 HTML 链接打开嵌入的 PDF

android - 如何在android xamarin中调整图像按钮的大小?

c# - 如何以 xamarin 形式动态更改 CultureInfo

c# - Xamarin.Forms 中的 MVVMLight 的 EventToCommand

Visual Studio 2015 中未检测到 Android 手机