创建自定义按钮渲染器时覆盖转换错误 [英] Override conversion errors when creating custom button renderer

查看:32
本文介绍了创建自定义按钮渲染器时覆盖转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖 Xamarin Forms 中 Android 按钮渲染器的默认行为.我利用了(不是偷来的;)来自 https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual/create 并创建了这个:

I am trying to override the default behavior of the Android button renderer in Xamarin Forms. I have leveraged (not stolen;) the source code from https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual/create and have created this:

namespace Patron.Droid.Renderers
{
    public class MixedCaseButtonRenderer : Xamarin.Forms.Platform.Android.AppCompat.ButtonRenderer
    {
        public MixedCaseButtonRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Button> el)
        {
            base.OnElementChanged(el);

            if (el.OldElement != null)
            {
                // Cleanup
            }

            if (el.NewElement != null)
            {
                var button = (Button)this.Control;
                button.SetAllCaps(false);
            }
        }
    }
}

但是当我构建它时,我收到以下错误

But when I build it, I get the following errors

Severity    Code    Description Project File    Line    Suppression State
Error   CS0115  'MixedCaseButtonRenderer.OnElementChanged(ElementChangedEventArgs<Button>)': no suitable method found to override   Patron.Android  C:\Users\ThomH\workspace\patron\Patron\Patron\Patron.Android\Renderers\MixedCaseButtonRenderer.cs   22  Active
Error   CS0311  The type 'Android.Widget.Button' cannot be used as type parameter 'TElement' in the generic type or method 'ElementChangedEventArgs<TElement>'. There is no implicit reference conversion from 'Android.Widget.Button' to 'Xamarin.Forms.Element'.  Patron.Android  C:\Users\ThomH\workspace\patron\Patron\Patron\Patron.Android\Renderers\MixedCaseButtonRenderer.cs   22  Active
Error   CS1503  Argument 1: cannot convert from 'Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Android.Widget.Button>' to 'Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.Button>'   Patron.Android  C:\Users\ThomH\workspace\patron\Patron\Patron\Patron.Android\Renderers\MixedCaseButtonRenderer.cs

推荐答案

Button 存在于 Android.WidgetXamarin.Forms 中.您需要指定要使用的命名空间

Button exists in both Android.Widget and Xamarin.Forms. You need to specify which namespace you mean to use

这篇关于创建自定义按钮渲染器时覆盖转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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