【发布时间】:2014-11-07 04:05:09
【问题描述】:
我是第一次使用 Xamarin,但我仍在掌握一些基础知识。
请看下面的例子
protected override void OnCreate(Bundle bundle)
{
try
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Login);
Button button = FindViewById<Button>(Resource.Id.Login);
button.Click += delegate
{
throw new Exception("button exception");
};
}
catch(Exception e)
{
}
}
我已经简化了上面的代码以直截了当。
我正在为我的活动内部设置一些通用错误处理,例如,我在单击按钮时引发了异常。尽管这被包装在 try/catch 中,异常被作为“未处理的异常”抛出。
希望有人能解释这段代码是如何在逻辑上运行的,我假设这是某种线程问题?我将如何最好地处理这种情况?
最终目标是我希望通过一些内部调用来登录;返回任何错误消息,然后使用消息框将其冒泡。不幸的是,我似乎无法捕捉到它们。
谢谢。
我已尝试设置一个全局处理程序,如以下代码所示:
protected override void OnCreate(Bundle bundle)
{
try
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
base.OnCreate(bundle);
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
{
throw new Exception("Invalid account code or password. Please try again.");
};
}
catch(Exception e)
{
}
}
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ExceptionObject.ToString());
Console.WriteLine("Press Enter to continue");
Console.ReadLine();
throw new Exception("AH HA!");
}
【问题讨论】: