JMK*_*JMK 8 c# xaml xamarin xamarin.forms
好的,我有以下观点:
<?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>
Run Code Online (Sandbox Code Playgroud)
此视图的BindingContext是以下ViewModel:
public class MainViewModel
{
public MainModel Model { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是MainModel:
public class MainModel
{
public List<MyPoco> MyPocos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是MyPoco:
public class MyPoco
{
public string MyString { get; set; }
public int MyInt { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是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;
Run Code Online (Sandbox Code Playgroud)
真的没别的,我得到以下异常:
指定演员表无效.
但是没有内部异常,也没有更多的上下文,据我所知,我正在做正确的事情.
绑定到字符串列表工作正常,当我用任何其他对象替换出错的时候.
关于我哪里出错的任何想法?
谢谢
JMK*_*JMK 23
转而你不能将Label直接放在DataTemplate中,而是必须将它嵌套在ViewCell中,如下所示:
<ViewCell>
<ViewCell.View>
<Label Text="{Binding MyString}" />
</ViewCell.View>
</ViewCell>
Run Code Online (Sandbox Code Playgroud)
谜团已揭开.