【问题标题】:Xamarin Forms error diagnostic when laying out root page (Platform.LayoutRootPage)布局根页面时 Xamarin Forms 错误诊断 (Platform.LayoutRootPage)
【发布时间】: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


【解决方案1】:

有没有办法获得更多信息?

你可以使用:Diagnostic MSBuild Output

这是图片:

但是当异常发生时,这些事件都不会被触发。

如果错误是你的XAML有问题,那是编译错误,它不会触发你的ExceptionHandlers

【讨论】:

  • 这不是构建问题。这是一个运行时问题。应用程序构建正确,但是当它启动时(无论是在模拟器还是设备上),它都会崩溃并报告引用未设置异常。
  • @spatialguy,你能分享一个诊断 MSBuild 输出吗?我们需要查看一些错误日志来帮助您查明问题。
  • @spatialguy,您是否尝试过使用调试器来查找崩溃的行?
  • 有人找到解决方案了吗??
猜你喜欢
  • 2017-12-29
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
  • 2018-08-23
  • 2016-02-17
  • 2023-03-16
  • 2012-09-12
  • 2014-09-13
相关资源
最近更新 更多