4

为了能够创建需要在 Xamarin 环境中本地完成的样式,我使用了自定义渲染器。在 android 端,我收到一个System.InvalidCastException,我不知道如何解决这个问题。我目前正在包装一个Android.Widget.StackView由于这个家伙对我之前遇到的错误的建议(没有默认构造函数采用 0 参数错误)。该错误似乎不是由于声明或实例化而发生的,而是当我将其添加到 ContentView 并在返回时发生(通过注释掉如下所示的行来找到)。

        MainPage = new ContentPage {
            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    item1,
                    item2,
                    item3,
                    item4,
                    item5,
                    item6,
                    //rendererIWantToAdd,//taking out this line make it work. adding it breaks it
                }
            }
        };

代码

渲染视图

public class RendererView :  View
{

    public static readonly BindableProperty ImageProperty = BindableProperty.Create(
        propertyName: "Src",
        returnType: typeof(string),
        declaringType: typeof(string),
        defaultValue: ""
    );
    public String Src
    {
        get { return (string)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    public static readonly BindableProperty TextProperty = BindableProperty.Create(
        propertyName: "Text",
        returnType: typeof(string),
        declaringType: typeof(string),
        defaultValue: "Hello World"
    );
    public String Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly BindableProperty ColorProperty = BindableProperty.Create(
        propertyName: "Color",
        returnType: typeof(string),
        declaringType: typeof(string),
        defaultValue: "#AAAAAA"
    );
    public String Color
    {
        get { return (string) GetValue(ColorProperty); }
        set { SetValue(ColorProperty, value); }
    }
}

RendererViewAndroid

[assembly: ExportRenderer(typeof(Project.RendererView), typeof(stackViewWrapper))]
namespace Project.Droid
{
    public class RendererViewAndroid : ViewRenderer<RendererView, stackViewWrapper>
    {

        Android.Widget.TextView label;
        Android.Widget.ImageView image;

        protected override void OnElementChanged(ElementChangedEventArgs<RendererView> e)
        {
            base.OnElementChanged(e);
            if (Control == null)
            {
                createItem(e);
            }

            if (e.OldElement != null)
            {

                unsubscribe(e);
            }

            if (e.NewElement != null)
            {
                subscribe(e);
            }



        }


        protected void createItem(ElementChangedEventArgs<RendererView> e)
        {


            SetNativeControl(new stackViewWrapper());
            label = new Android.Widget.TextView(Context)
            {
                Text = Element.Text
            };

            image = new Android.Widget.ImageView(Context);
            if (!string.IsNullOrWhiteSpace(Element.Src))
            {
                Context.Resources.GetBitmapAsync(Element.Src).ContinueWith((t) =>
                {
                    var bitmap = t.Result;
                    if (bitmap != null)
                    {
                        image.SetImageBitmap(bitmap);
                        bitmap.Dispose();
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());

            }
            else {
            //  // clear the image
                image.SetImageBitmap(null);
            }


            Control.AddView(image);
            Control.AddView(label);



        }
        protected void subscribe(ElementChangedEventArgs<RendererView> e)
        {

        }
        protected void unsubscribe(ElementChangedEventArgs<RendererView> e)
        {

        }





        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }





    }


    public  class stackViewWrapper : Android.Views.View
    {
        public stackViewWrapper() : base(Android.App.Application.Context)
        {
        }
    }
}

错误堆栈跟踪

System.InvalidCastException:指定的强制转换无效。在 (wrapper castclass) System.Object:__castclass_with_cache (object,intptr,intptr) 在 Xamarin.Forms.Registrar1[TRegistrable].GetHandler (System.Type type) [0x0001b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Registrar.cs:24 at Xamarin.Forms.Registrar1[TRegistrable].GetHandler[TOut](System.Type 类型)[0x00000] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Registrar.cs:29 在 Xamarin.Forms.Platform.Android.Platform。 CreateRenderer (Xamarin.Forms.VisualElement 元素) [0x00006] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:281 在 Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin. Forms.VisualElement 视图,IVisualElementRenderer oldRenderer,Xamarin.Forms.Platform.Android.RendererPool 池,布尔值 sameChildren) [0x00023] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:72 在 Xamarin .Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] 在 C 中:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:204 在 Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms .Platform.Android\VisualElementPackager.cs:56 在 Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:327 at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].SetElement(Xamarin.Forms.Platform.Android.TElement 元素)[0x00111] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:195 在 Xamarin.Forms.Platform .Android.VisualElementRenderer 1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:128 at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:282 at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, Boolean sameChildren) [0x00023] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:72 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:204 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:56 at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:327 在 Xamarin .Forms.Platform.Android.VisualElementRenderer1[TElement].SetElement (Xamarin.Forms.Platform.Android.TElement element) [0x00111] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:195 at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement(Xamarin.Forms.VisualElement 元素)[0x00027] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:128在 Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement 元素)[0x0001f] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:282 在 Xamarin.Forms .Platform.Android.Platform.AddChild(Xamarin.Forms.VisualElement 视图,布尔布局)[0x00015] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:518 在 Xamarin.Forms。 Platform.Android.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x00089] 在 Xamarin.Forms.Platform.Android 的 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:406。FormsApplicationActivity.InternalSetPage(Xamarin.Forms.Page 页面)[0x000b7] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\FormsApplicationActivity.cs:286 在 Xamarin.Forms.Platform.Android.FormsApplicationActivity.SetMainPage ( ) [0x00000] 在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\FormsApplicationActivity.cs:305 在 Xamarin.Forms.Platform.Android.FormsApplicationActivity.LoadApplication (Xamarin.Forms.Application 应用程序)[0x0002d]在 C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\FormsApplicationActivity.cs:108 在 Project.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x000ae] 在 /Users/ryan/project- app-xamarin/Project/Project.Droid/MainActivity.cs:47 在 Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this,IntPtr native_savedInstanceState) [0x00011] 在 /Users/builder/data/lanes/3340/4e275588/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Activity.cs:2426 在在(包装器动态方法) System.Object:688deba4-7150-4f17-9062-10e53a94164c (intptr,intptr,intptr)

4

1 回答 1

1

不确定这是否真的是您问题的根源,但如果不是,它肯定会在之后给您带来问题。

在您的RendererView所有绑定属性中,应将其declaringType设置为声明它们的类,即RendererView.

例如,您的第一个绑定属性应如下所示:

public static readonly BindableProperty ImageProperty = BindableProperty.Create(
    propertyName: "Src",
    returnType: typeof(string),
    declaringType: typeof(RendererView),
    defaultValue: ""
);

然后对所有其他人做同样的事情。如果这不能解决它,一定要让我知道。

编辑:根据下面的OP评论,最初的错误来自该ExportRenderer行,需要声明RendererView而不是stackViewWrapper这样:

[assembly: ExportRenderer(typeof(Project.RendererView), typeof(RendererViewAndroid))]
于 2016-06-20T20:01:40.563 回答