努力理解Xamarin异常处理 [英] Struggling to understand Xamarin exception handling

查看:362
本文介绍了努力理解Xamarin异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在抓取互联网中的解决方案的希望相当长的时间,我已经遇到了一些答案,但没有这些似乎达到我想要的。

I have been crawling the Internet for quite a long time in hope of a solution, and I've come across a number of answers, but none of these seem to achieve what I want.

我想处理异常,而不会导致应用程序崩溃。而不是应用程序简单地退出,我宁愿捕捉例外,以更加用户友好的错误(也许一个消息警告)呈现给用户,让他们继续在应用程序运行。

I'm trying to handle exceptions without causing the app to crash. Rather than the app simply exiting, I would rather capture the exception, present the user with a more user-friendly error (perhaps a messagebox warning) and allow them to continue operation in the app.

是否有可能阻止救助应用

Is it possible to stop the app from bailing out?

目前,我正在试图赶上这就像以下方式:

The way I'm currently attempting to catch this is like the following:

public class Login : Activity
{
    int count = 1;
    Session mySession;

   protected override void OnCreate(Bundle bundle)
    {
        AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;

            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Login);


            Button button = FindViewById<Button>(Resource.Id.Login);
            string accountCode = Resource.Id.AccountCode.ToString();
            string password = Resource.Id.Password.ToString();

            // button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
            button.Click += delegate
            {
                    throw new Exception("LETS THROW A RANDOM EXCEPTION");

            };

    }


    void HandleAndroidException(object sender, RaiseThrowableEventArgs e)
    {
        Log.Error("INTERNAL DEBUG", "PLEASE HANDLE MY EXCEPTION!");
        e.Handled = true;
        System.Console.Write("YOU'VE JUST BEEN HANDLED!");
    }
}



正如你可以看到我举办了一个一般例外和试图用UnhandledExceptionRaiser赶上这一点。我用这个作为参考:的http:// androidapi。 xamarin.com/index.aspx?link=E%3AAndroid.Runtime.AndroidEnvironment.UnhandledExceptionRaiser

我能找到我的消息中的Android设备记录工具,但它是被触发后发生未处理的异常错误。我认为,这意味着里面的Xamarin是一种在例外第一条裂缝和翻倒的东西。当然,必须有停止该??

I am able to find my message in the "Android Device Logging" tool, however it is being triggered AFTER an unhandled exception error occurs. I think this means something inside of Xamarin is having first crack at the exception and falling over. Surely there has to be a way of stopping this??

我见过无数的例子在网上,人们一直在问类似的问题,至今没有明确的方案。有些人提出了一些解决方案,但这些并没有真正做我的预期。

I have seen countless examples online where people have been asking similar questions and there has been no clear solution. Some people have offered some solutions, but these don't actually do what I had anticipated.

这是字面上超乎想象到我,如果这不能这样做。

It is literally mind boggling to me if this cannot be done.

这是使用Xamarin,也是我第一次开发移动应用程序,所以我道歉,如果我是无知不谈我的第一次。

This is my first time using Xamarin and also my first time developing a mobile app so I apologise if I'm being ignorant about anything.

请帮助!

推荐答案

有是你必须了解有关的性质一件重要的事情<在Android的code>未处理例外,没有一个....在Android的它是一个未捕获除外,这意味着你可以' T处理,或像你也许会在.NET环境中恢复它。 Xamarin(单声道)内部通过字面上与周围的try-catch一切,提高了未处理的事件处理这些未捕获的异常但这是除了点。它也气馁与UI以及交互各种原因。
理论上有几种变通方法用于显示一个对话框,向用户或重新启动应用程序,其中没有我建议你做。相反,你应该围绕敏感区域与的try-catch子句来处理预期的异常,作为出乎意料的只是使用异常报告组件和分析报告的异常之后,更新您的应用。

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.

另外,我想移动事件订阅的应用类,但是这是一个个人喜好。
,如下所示:

Also, I would move the event subscription to the Application class but that is a personal preference. Like so:

public class YourAppClass : Application
{
    public override void OnCreate()
    {
        AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException;
    }
}

这篇关于努力理解Xamarin异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆