Ash*_*aar 21 cross-platform xamarin.ios xamarin.android xamarin xamarin.forms
你能告诉我如何在Xamarin Cross平台项目中处理全局异常(没有App崩溃).
Ger*_*uis 45
我知道,没有'Xamarin.Forms'这样做的方式.你需要挂钩Android和iOS,你可以做的是创建一个方法,以同样的方式处理它们.
关于这一点的一篇好文章是彼得·诺曼.他描述了要在Android中实现它,你可以在你的MainActivity.cs.
// In MainActivity
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
Xamarin.Forms.Forms.Init(this, bundle);
DisplayCrashReport();
var app = new App();
LoadApplication(app);
}
?#?region? Error handling
private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
{
var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
LogUnhandledException(newExc);
}
private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
LogUnhandledException(newExc);
}
internal static void LogUnhandledException(Exception exception)
{
try
{
const string errorFileName = "Fatal.log";
var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // 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);
// Log to Android Device Logging.
Android.Util.Log.Error("Crash Report", errorMessage);
}
catch
{
// just suppress any error logging exceptions
}
}
/// <summary>
// If there is an unhandled exception, the exception information is diplayed
// on screen the next time the app is started (only in debug configuration)
/// </summary>
[Conditional("DEBUG")]
private void DisplayCrashReport()
{
const string errorFilename = "Fatal.log";
var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var errorFilePath = Path.Combine(libraryPath, errorFilename);
if (!File.Exists(errorFilePath))
{
return;
}
var errorText = File.ReadAllText(errorFilePath);
new AlertDialog.Builder(this)
.SetPositiveButton("Clear", (sender, args) =>
{
File.Delete(errorFilePath);
})
.SetNegativeButton("Close", (sender, args) =>
{
// User pressed Close.
})
.SetMessage(errorText)
.SetTitle("Crash Report")
.Show();
}
?#?endregion?
Run Code Online (Sandbox Code Playgroud)
对于iOS,您可以在您的网站中添加这样的代码AppDelegate.cs.
//iOS: Different than Android. Must be in FinishedLaunching, not in Main.
// In AppDelegate
public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary options)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
// Rest of your code...
}
/// <summary>
// If there is an unhandled exception, the exception information is diplayed
// on screen the next time the app is started (only in debug configuration)
/// </summary>
[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();
}
Run Code Online (Sandbox Code Playgroud)
它还包括在调试应用程序时显示日志的功能.当然,您可以实现自己的日志记录或处理方法.你可以看到的一件事是HockeyApp.这会默认处理未处理的异常,并将其发送给您,以及其他内容.
更新,因为仍然可以在Google上找到:对于崩溃报告和分析,您现在想要开始查看App Center.这是HockeyApp和Xamarin Insights(包括构建,分发和推送通知等)的演变,现在充当了与应用程序有关的一切任务仪表板,而不仅仅是Xamarin.
对于UWP和WinPhone 8.1 UnhandledException,Application对象中应该有一个处理程序.查看此答案以获取更多信息.我引用:
对于基于XAML的应用程序,您可以使用UnhandledException ; 但是,它只捕获通过XAML(UI)框架出现的异常,并且您并不总是获得有关根本原因的大量信息,即使在InnerException中也是如此.
Windows 8.1更新:UnhandledException还将捕获异步void方法创建的异常.在Windows 8中,此类异常只会使应用程序崩溃.LunarFrog 在他们的网站上对此进行了很好的 讨论.
基本上你应该在你的构造函数中添加事件处理程序App()中App.xaml.cs:this.UnhandledException += (o, s) => {}.
| 归档时间: |
|
| 查看次数: |
14075 次 |
| 最近记录: |