创建自定义按钮渲染器时覆盖转化错误

问题描述

我试图覆盖Xamarin Forms中Android按钮渲染器的认行为。我已经利用(未盗用)https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/visual/create的源代码并创建了这个代码

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);
            }
        }
    }
}

但是在构建它时,出现以下错误

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中。您需要指定要使用的命名空间