gpt4 book ai didi

c# - Xamarin.Forms-为什么 NavigationPage 的 CustomRenderer Android 抛出 InvalidCastException?

转载 作者:太空狗 更新时间:2023-10-29 13:59:00 26 4
gpt4 key购买 nike

我尝试在 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 更新 2

编辑:

这是调用栈;

>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

最佳答案

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

注意类名,它是“AppCompat.NavigationPageRenderer”。

关于c# - Xamarin.Forms-为什么 NavigationPage 的 CustomRenderer Android 抛出 InvalidCastException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com