问题描述
我正在学习Xamarin.Forms和MVVM模式,其中包括数据绑定。我不明白为什么在第二个标签之后,什么都没有显示,但是Str_Winner遇到了断点,并且该字符串的内容与我期望的值相同。
Page_History.xaml如下:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local1="clr-namespace:TicTacToe.viewmodel"
x:Class="TicTacToe.View.Page_History">
<ContentPage.Resources>
<ResourceDictionary>
<local1:VM_GameInfo x:Key="VM_GameInfo"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout BindingContext="{StaticResource VM_GameInfo}">
<ListView ItemsSource="{Binding GameHistory}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Str_Title}" />
<Label Text="{Binding SaveDateTime}" />
<Label Text="{Binding Str_Winner}" />
<Button Text="Something"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
我的代码在这里:https://drive.google.com/file/d/1w1TL0am6VeS2dLgsesiQT_S2h0L7Lo1H/view?usp=sharing
解决方法
给RowHeight="150"
或将HasUnevenRows="True"
设置为listView:
<ListView ItemsSource="{Binding GameHistory}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Label Text="{Binding Str_Title}" />
<Label Text="{Binding SaveDateTime}" />
<Label Text="{Binding Str_Winner}" />
<Button Text="Something"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>