我有一个自定义视图,我尝试将其作为List<T>
传递给。出于某种原因,在尝试加载页面时,应用会抛出System.ArrayTypeMismatchException
。
这是班级:
public class DiaryCalendarCustomView : View
{
MiscFunctions misctools = new MiscFunctions();
private List<DiaryNextContactEventModel> _eventList = new List<DiaryNextContactEventModel>();
public List<DiaryNextContactEventModel> EventList
{
get { return _eventList; }
set { _eventList = value; }
}
public void SetSelectedDate (DateTime selectedDate)
{
SelectedDate = selectedDate;
Settings.Current.NextContactContactDate = selectedDate.ToLocalTime();
}
public DateTime SelectedDate { get; set; }
public DiaryCalendarCustomView()
{
}
}
查看型号:
private List<DiaryNextContactEventModel> _eventList = new List<DiaryNextContactEventModel>();
public List<DiaryNextContactEventModel> EventList
{
get { return _eventList; }
set { SetProperty(ref _eventList, value); }
}
当我将静态数据添加到EventList对象时,它工作正常,当我从XAML视图中删除Binding时,它也可以正常工作。所以问题似乎是xamarin试图将我的列表转换为另一种类型的可枚举以及它失败的地方。
XAML:
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.Children>
<partials:DiaryCalendarCustomView EventList="{Binding EventList}"/>
</Grid.Children>
</Grid>
调试和搜索并没有真正提供任何有用的东西。任何帮助将不胜感激。
答案 0 :(得分:1)
如果要在XAML中使用自定义属性,则需要在视图中声明它。你的代码看起来很好,只需按照这样的教程:Creating Custom Controls with Bindable Properties in Xamarin.Forms并添加缺少的部分,所以属性definiton和propertyChanged方法:
public static readonly BindableProperty EventListProperty = BindableProperty.Create(
propertyName: "EventList",
returnType: typeof(List<DiaryNextContactEventModel>),
declaringType: typeof(DiaryCalendarCustomView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: EventListPropertyChanged);
还有:
private static void EventListPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = (DiaryCalendarCustomView) bindable;
view.EventList = (List<DiaryNextContactEventModel>) newValue;
}
还要确保您的类实现了INotifyPropertyChanged接口,因此当您在EventListPropertyChanged中更改EventList时,视图将重新加载