绑定到 POCO 列表时,指定的演员表无效

Specified cast is not valid when binding to list of POCO's

好的,所以我有以下看法:

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

这是主模型:

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;

真的没有别的,我收到以下异常:

Specified cast is not valid.

但没有内部异常,也没有更多上下文,据我所知,我做的一切都是正确的。

绑定到字符串列表工作正常,当我用任何其他对象替换它时出现问题。

关于我哪里出错的任何想法?

谢谢

事实证明,您不能将 Label 直接放在 DataTemplate 中,而必须将其嵌套在 ViewCell 中,如下所示:

<ViewCell>
    <ViewCell.View>
        <Label Text="{Binding MyString}" />
    </ViewCell.View>
</ViewCell>

谜底已解。