【发布时间】:2018-04-29 21:52:54
【问题描述】:
在处理用 Xamarin Forms 编写的应用程序时,我经常看到以下导致应用程序崩溃的异常:
11-16 11:00:40.009 I/MonoDroid(16094): UNHANDLED EXCEPTION:
11-16 11:00:40.113 I/MonoDroid(16094): System.NullReferenceException: Object reference not set to an instance of an object.
11-16 11:00:40.113 I/MonoDroid(16094): at Xamarin.Forms.Platform.Android.AppCompat.Platform.LayoutRootPage (Xamarin.Forms.Page page, System.Int32 width, System.Int32 height) [0x0000c] in D:\agent\_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:290
11-16 11:00:40.113 I/MonoDroid(16094): at Xamarin.Forms.Platform.Android.AppCompat.Platform.Xamarin.Forms.Platform.Android.IPlatformLayout.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00003] in D:\agent\_work\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:199
11-16 11:00:40.113 I/MonoDroid(16094): at Xamarin.Forms.Platform.Android.PlatformRenderer.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x0000e] in D:\agent\_work\1\s\Xamarin.Forms.Platform.Android\PlatformRenderer.cs:73
11-16 11:00:40.113 I/MonoDroid(16094): at Android.Views.ViewGroup.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00008] in <e975227ac8644a30bb0866117325de0d>:0
11-16 11:00:40.113 I/MonoDroid(16094): at (wrapper dynamic-method) System.Object:55caff06-7828-42f0-9580-cfb6673c4b5a (intptr,intptr,bool,int,int,int,int)
有时 XAML 中存在问题,有时,视图模型绑定(通过附加属性)存在问题,但通常我需要花费大量时间才能找出问题所在。有没有办法获取更多信息? Xamarin.Forms 是否有详细的日志记录,可以打开以获取更多诊断信息或类似信息?
另外,在 MainActivity 中,我有以下代码(使用 Serilog 进行日志记录):
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
...
RegisterExceptionHandlers();
...
}
private void RegisterExceptionHandlers()
{
TaskScheduler.UnobservedTaskException += UnobservedException;
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
AndroidEnvironment.UnhandledExceptionRaiser += UnhandledExceptionRaiser;
}
private void UnhandledException(object source, UnhandledExceptionEventArgs args)
{
Log.Error("Domain Exception from {source}: {exception}", source, args.ExceptionObject?.ToString() ?? "N/A");
}
private void UnobservedException(object source, UnobservedTaskExceptionEventArgs args)
{
Log.Error("TaskSchedulerException: {source} {exception}", source, args);
if (!args.Observed)
args.SetObserved();
}
private void UnhandledExceptionRaiser(object source, RaiseThrowableEventArgs args)
{
Log.Error("Exception from {source}: {exception}", source, args.Exception?.ToString() ?? "N/A");
}
但是当异常发生时,这些事件都不会被触发。有人知道为什么吗?
【问题讨论】:
-
有任何解决方案@spatialguy??
标签: c# xamarin.forms xamarin.android