Xamarin-Custom Renderer找不到适合的方法来覆盖

B. *_*hof 2 c# android xamarin xamarin.forms

因此,当我尝试使用textOn和textOff函数进行自定义开关时,我一直在Xamarin中遇到此错误。我查找了此线程:https : //forums.xamarin.com/discussion/26694/problem-creating-custom-renderer。而且仍然无法弄清楚。这也是我第一次为Xamarin创建自定义控件,因此我可能会犯一些初学者的错误

这是我的自定义控件的代码:

[assembly:ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]

namespace SalesKicker
{
public class CustomSwitch : Android.Widget.Switch
{
    public static readonly BindableProperty TextOnProperty = BindableProperty.Create<CustomSwitch, string>(p => p.TextOn, AppResources.CustomSwitch_DefaultTextOn);
    public static readonly BindableProperty TextOffProperty = BindableProperty.Create<CustomSwitch, string>(p => p.TextOff, AppResources.CustomSwitch_DefaultTextOff);

    public string TxtOn
    {
        get { return (string)GetValue(TextOnProperty); }
        set { SetValue(TextOnProperty, value); }
    }

    public string TxtOff
    {
        get { return (string)GetValue(TextOffProperty); }
        set { SetValue(TextOffProperty, value); }
    }
}

public class CustomSwitchRenderer : SwitchRenderer
{
//the error is being thrown here: Error CS0115: 'CustomSwitchRenderer.OnElementChanged(ElementChangedEventArgs<CustomSwitch>)': no suitable method found to override (CS0115) (SalesKicker.Droid)
    protected override void OnElementChanged(ElementChangedEventArgs<SalesKicker.CustomSwitch> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || this.Element == null)
        {
            return;
        }

        var customSwitch = this.Element;

        var control = new Switch(Forms.Context)
            {
                TextOn = customSwitch.TxtOn,
                TextOff = customSwitch.TxtOff
            };

        this.SetNativeControl(control);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我我在这里做错了吗?

Sve*_*übe 5

参数的类型是函数签名的一部分。如果覆盖功能,则它们必须具有相同的签名。使用ElementChangedEventArgs<Switch>

protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
Run Code Online (Sandbox Code Playgroud)