viewmodel, model and code behind
你好社区,
需要帮助数据绑定识别XAML中ListView中的MVVM模式 . 我不知道为什么它不起作用 . 我在视图中看不到任何数据 . 我通过调试得到了这个例外 .
Xamarin.Forms.Xaml.XamlParseException:位置8:10 . 属性内容为null或不是IEnumerable
谢谢你的回答 .
XAML
<ViewCell>
<StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal">
<StackLayout HorizontalOptions="Start" Orientation="Vertical">
<Label Text="{Binding Name}" />
</StackLayout>
<StackLayout HorizontalOptions="End" Orientation="Vertical">
<Label Text="{Binding Information}" />
</StackLayout>
</StackLayout>
</ViewCell>
Code
public partial class PartyStatusPage : ContentPage
{
public PartyStatusPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
BindingContext = new PartyStatusPageViewModel();
}
}
public class PartyStatusPageViewModel
{
public List<PartyStatus> ItemList;
public PartyStatusPageViewModel()
{
ItemList = new List<PartyStatus>();
ItemList.Add(new PartyStatus { Name = "Max", Information = "test" });
}
}
public class PartyStatus
{
public string Name { get; set; }
public string Information { get; set; }
public bool State { get; set; }
}
2 回答
您的XAML中的DataTemplate是错误的 . 您需要指定
ViewCell
explicit的视图 . 查看以下XAML:例如XAML:
ViewModel(您需要在更改值后刷新var)
[编辑]或您的视图将无法识别/显示任何内容[编辑]