涉及的数据类型和自定义控件:
我已经定义了我自己的类型,它是一个"IList bool",并且有它自己的索引器。该类存储是否在某一天重复某事,即如果Data[2] 为真,则暗示应在星期三重复某事。以下是部分代码
public class WeeklyDayPresence : INotifyCollectionChanged, IList<bool>, ISerializable
{
private List<bool> Data { get; set; }
public bool this[int index]
{
get => Data[index];
set
{
bool temp = Data[index];
Data[index] = value;
CollectionChanged?.Invoke(this,new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,value,temp,index));
}
}
}
现在,我打算有一个选择系统,允许用户打开或关闭绑定到某一天的按钮,这样他们就可以选择哪些日子应该是真或假。
因此,我创建了一个控件(“DayOfWeekSelector”),它本质上是 7 个非常相似的自定义按钮的堆栈布局。这是自定义按钮的代码:
public class DayOfWeekButton : Button
{
public static readonly BindableProperty SelectedProperty =
BindableProperty.Create("Selected", typeof(bool), typeof(bool), false);
public bool Selected
{
get => (bool) GetValue(SelectedProperty);
set
{
SetValue(SelectedProperty, value);
RefreshColours();
}
}
public DayOfWeekButton()
{
RefreshColours();
Clicked += DayOfWeekButton_Clicked;
}
private void DayOfWeekButton_Clicked(object sender, EventArgs e)
{
Selected = !Selected;
}
}
在我的 DayOfWeekSelector 中,我传入了一个“具有”WeeklyDayPresence 的对象:
public class EventOccurrenceRepeater : NotifyModel, ISerializable
{
private WeeklyDayPresence _repeatOnDay;
public WeeklyDayPresence RepeatOnDay
{
get => _repeatOnDay;
set => SetValue(ref _repeatOnDay, value);
}
}
问题:
当我尝试将值绑定到 Button 时,我收到一个System.Reflection.TargetParameterCountException。
private void AddButton(string text, int ID)
{
var button = new DayOfWeekButton {Text = text};
var binding = new Binding($"RepeatOnDay[{ID}]", BindingMode.TwoWay);
button.SetBinding(DayOfWeekButton.SelectedProperty, binding);
button.BindingContext = Repeater; // Throws Exception after this
//...
}
那是什么异常,为什么我会得到它?如果有帮助,我已附加到堆栈跟踪的链接