涉及的数据类型和自定义控件:
我已经定义了自己的类型,它是“ IList bool” ,并且具有自己的索引器。此类存储是否在某天重复某件事,即,如果Data [2]为true,则意味着应该在星期三重复某件事。下面是部分代码
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
//...
}
那是什么例外,我为什么要得到它?如果有帮助,我已经在堆栈跟踪中附加了link