因此,当我尝试使用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);
}
}
}
有人可以告诉我这里我做错了吗?
答案 0 :(得分:5)
参数的类型是function signature的一部分。如果覆盖功能,则必须具有相同的签名。使用ElementChangedEventArgs<Switch>
。
protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
答案 1 :(得分:0)
尝试更改为此
using System;
using Xamarin.Forms;
namespace SalesKicker
{
public class CustomSwitch : 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); }
}
}
}
Xamarin.Forms项目中的这个类
然后,
[assembly:ExportRenderer(typeof(CustomSwitch), typeof(CustomSwitchRenderer))]
namespace SalesKicker
{
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<Switch> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || this.Element == null)
return;
var customSwitch = this.Element;
var control = new Switch(Forms.Context)
{
TextOn = customSwitch.TextOn,
TextOff = customSwitch.TextOff
};
this.SetNativeControl(control);
}
}
}
这在Android项目中。如果您有任何进一步的问题,请告诉我。这应该适合你。