好的,我有以下观点:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="BoomSauce.MainPage">
<ListView ItemsSource="{Binding Model.MyPocos}">
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding MyString}"></Label>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
此视图的BindingContext是以下ViewModel:
public class MainViewModel
{
public MainModel Model { get; set; }
}
这是MainModel:
public class MainModel
{
public List<MyPoco> MyPocos { get; set; }
}
这是MyPoco:
public class MyPoco
{
public string MyString { get; set; }
public int MyInt { get; set; }
}
以下是App()
中发生的事情MainPage = new MainPage();
var viewModel = new MainViewModel
{
Model = new MainModel
{
MyPocos = new List<MyPoco>()
{
new MyPoco() { MyInt = 1, MyString = "a" },
new MyPoco() { MyInt = 2, MyString = "b" },
new MyPoco() { MyInt = 3, MyString = "c" },
new MyPoco() { MyInt = 4, MyString = "d" },
new MyPoco() { MyInt = 5, MyString = "e" }
}
}
};
MainPage.BindingContext = viewModel;
真的没别的,我得到以下例外:
指定的演员表无效。
但是没有内部异常,也没有更多的背景,据我所知,我正在做正确的事情。
绑定到字符串列表工作正常,当我用任何其他对象替换时出错了。
关于我哪里出错的任何想法?
由于
答案 0 :(得分:23)
转而你不能将Label直接放在DataTemplate中,而是必须将它嵌套在ViewCell中,如下所示:
<ViewCell>
<ViewCell.View>
<Label Text="{Binding MyString}" />
</ViewCell.View>
</ViewCell>
神秘解决了。