迁移到FormsApplicationActivity导致NavigationRenderer中的异常 [英] Migrating to FormsApplicationActivity causing exceptions in NavigationRenderer

查看:109
本文介绍了迁移到FormsApplicationActivity导致NavigationRenderer中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我将AndroidActivity替换为FormsAppCompatActivity.

In my app I replaced AndroidActivity with FormsAppCompatActivity.

现在在扩展NavigationRenderer的自定义渲染器中,我得到了一个跟踪到方法Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync的异常:

Now in a custom renderer extending NavigationRenderer, I get an exception that's traced to method Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Cannot cast from source type to destination type.
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync (Xamarin.Forms.Page,bool,bool) <IL 0x000bd, 0x003fe>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.OnPushAsync (Xamarin.Forms.Page,bool) <IL 0x00004, 0x0003f>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.PushViewAsync (Xamarin.Forms.Page,bool) <IL 0x00003, 0x00036>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.<OnElementChanged>b__4_0 (Xamarin.Forms.Page) <IL 0x00003, 0x00033>
[MonoDroid] at Xamarin.Forms.EnumerableExtensions.ForEach<Xamarin.Forms.Page> (System.Collections.Generic.IEnumerable`1<Xamarin.Forms.Page>,System.Action`1<Xamarin.Forms.Page>) <0x000bf>
[MonoDroid] at Xamarin.Forms.Platform.Android.NavigationRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1<Xamarin.Forms.NavigationPage>) <IL 0x000ea, 0x006ff>
[MonoDroid] at Boats.Droid.PageFancyRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1<Xamarin.Forms.NavigationPage>) [0x00001] in d:\Code\Boats\Droid\Renderers\NavigationPageFancy.cs:33

由于我没有Xamarin.Forms的来源,所以我无法介入.该方法的反编译为此处

I cannot step into it since I don't have the source for Xamarin.Forms. The decompilation of that method is here

有什么想法吗?

推荐答案

这是因为AppCompat引入了一个新的NavigationRenderer,所以我应该在Xamarin.Forms.Platform.Android中对该类进行子类化.

This is because AppCompat introduces a new NavigationRenderer, so I should be subclassing that instead of the one in Xamarin.Forms.Platform.Android.

因此,解决方法是:

public class PageFancyRenderer : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer

代替:

public class PageFancyRenderer : Xamarin.Forms.Platform.Android.NavigationPageRenderer


为什么会发生此错误:


Why this error was happening:

在NavigationPageRenderer.SwitchContentAsync内部,有以下行:

Inside NavigationPageRenderer.SwitchContentAsync there is this line:

          ((Platform) this.Element.Platform).NavAnimationInProgress = false;

自从我将Android.NavigationRenderer子类化之后,哪个正在对Android.Platform类进行强制转换

Which is doing a cast to Android.Platform class since I was subclassing Android.NavigationRenderer

正确的渲染器Android.AppCompat.NavigationRenderer将转换为元素的正确平台(使用AppCompatActivity时),即Android.AppCompat.Platform

The correct renderer Android.AppCompat.NavigationRenderer would be casting to the correct platform of the element (when using AppCompatActivity) which is Android.AppCompat.Platform

如果Xamarin.Forms是开放源代码的,那么这将更容易捕获,因为我可以实际调试代码,但是相反,我必须查看IL反汇编以找到导致错误的行.

If Xamarin.Forms was open-souce, this would have been easier to catch because I would be able to actually debug the code, but instead, I had to look at the IL disassembly to find the line that was causing the error.

请为社区开源Xamarin.Forms

这篇关于迁移到FormsApplicationActivity导致NavigationRenderer中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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