Xamarin.Forms-为什么CustomRenderer Android for NavigationPage会抛出InvalidCastException?

时间:2016-05-13 15:21:43

标签: c# android xamarin xamarin.forms

我尝试在NavigationBar上为Xamarin.Forms android build设置背景图片。那里的例子几乎相同。我使用了这个自定义渲染器;

[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]
namespace xfprism.Droid
{

    public class CustomNavigationRenderer : NavigationRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);

            ExtendedActionBar();
        }

        void ExtendedActionBar()
        {
            var actionBar = ((Activity)Context).ActionBar;
            actionBar.SetIcon(Resources.GetDrawable(Resource.Drawable.icon));
            actionBar.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.coffee));
        }
    }
}

我的app.xaml.cs;

public partial class App
{
    protected override void OnInitialized()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
}

但是当我尝试调试项目时,我在行base.OnElementChanged(e);上得到了InvalidCastException。有什么我想念的吗?

仅供参考:我使用的是Xamarin 4.1.0.462

Visual Studio 2015 Update 2

编辑:

这是callstack;

>0xBE in Xamarin.Forms.Platform.Android.NavigationRenderer.SwitchContentAsync at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\NavigationRenderer.cs:196,4   
0x4 in Xamarin.Forms.Platform.Android.NavigationRenderer.OnPushAsync at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\NavigationRenderer.cs:124,4    
0x3 in Xamarin.Forms.Platform.Android.NavigationRenderer.PushViewAsync at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\NavigationRenderer.cs:34,4   
0x3 in Xamarin.Forms.Platform.Android.NavigationRenderer.<OnElementChanged>b__9_0 at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\NavigationRenderer.cs:97,41   
0x12 in Xamarin.Forms.EnumerableExtensions.ForEach<Xamarin.Forms.Page> at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Core\EnumerableExtensions.cs:38,5   
0xEA in Xamarin.Forms.Platform.Android.NavigationRenderer.OnElementChanged at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\NavigationRenderer.cs:97,4   
0x3 in xfprism.Droid.CustomNavigationRenderer.OnElementChanged at C:\Users\andrej\Desktop\xfprism-kahve\xfprism\xfprism.Droid\Nav_Renderer.cs:24,13 
0xFC in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.NavigationPage>.SetElement at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:190,4  
0x2E in Xamarin.Forms.Platform.Android.VisualElementRenderer<Xamarin.Forms.NavigationPage>.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:126,4    
0x21 in Xamarin.Forms.Platform.Android.Platform.CreateRenderer at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:284,4  
0x16 in Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:248,4    
0x91 in Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:237,4 
0xA2 in Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:355,4   
0xC in Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:429,4    
0x17E in Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication at C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:129,4  
0x2D in xfprism.Droid.MainActivity.OnCreate at C:\Users\andrej\Desktop\xfprism-kahve\xfprism\xfprism.Droid\MainActivity.cs:23,13    

1 个答案:

答案 0 :(得分:1)

如果您还没有解决方案; 尝试从Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer而不是Xamarin.Forms.Platform.Android.NavigationRenderer扩展/继承您的渲染器

注意班级名称,&#34; AppCompat.NavigationPageRenderer&#34;它是。