dimanche 27 janvier 2019

TargetParameterCountException thrown with Xamarin binding to custom indexer

The DataTypes and Custom Controls involved:

I have defined my own type, which is an "IList bool", and has its own indexer. This class stores whether to repeat something on a certain day, i.e. if Data[2] were true, then it would imply something should be repeated on a Wednesday. Below is part of the code

 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));
                }
            }
         }

Now, I intend to have a selection system that allows users to turn on or off buttons bound to a certain day, such that they can choose which days should be true or false.

I, therefore, created a control ("DayOfWeekSelector") that essentially is a stack layout of 7 very similar custom buttons. Here is the code for the custom button:

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;
        }
    }

In my DayOfWeekSelector, I pass in an object which "has a" WeeklyDayPresence:

public class EventOccurrenceRepeater : NotifyModel, ISerializable
    {
        private WeeklyDayPresence _repeatOnDay;
        public WeeklyDayPresence RepeatOnDay
        {
            get => _repeatOnDay;
            set => SetValue(ref _repeatOnDay, value);
        }
     }

The Problem:

When I try to bind the values to the Button, I receive a 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
            //...
        }

What is that exception and why am I getting it? I have attached a link to the stack trace if that helps





Aucun commentaire:

Enregistrer un commentaire