gpt4 book ai didi

c# - Xamarin - 自定义渲染器找不到合适的方法来覆盖

转载 作者:太空狗 更新时间:2023-10-29 16:32:05 25 4
gpt4 key购买 nike

因此,当我尝试使用 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);
}
}
}

有人可以告诉我我做错了什么吗?

最佳答案

参数的类型是function signature的一部分.如果您重写函数,它们必须具有相同的签名。使用 ElementChangedEventArgs<Switch> .

protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)

关于c# - Xamarin - 自定义渲染器找不到合适的方法来覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36566527/

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