【问题标题】:How come the 3rd label and the button is not showing up in the UI? [closed]为什么第三个标签和按钮没有出现在 UI 中? [关闭]
【发布时间】:2021-02-18 19:56:31
【问题描述】:

我正在学习 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

【问题讨论】:

  • 需要访问才能下载您的代码示例。
  • 请在此处添加相关代码。外部链接可能无法访问,或者那里的代码可能会更改,从而使这个问题对未来的读者毫无用处。

标签: c# xamarin mvvm data-binding


【解决方案1】:

给 listView 一个RowHeight="150" 或设置HasUnevenRows="True"

<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>

【讨论】:

  • 谢谢。 HasUnevenRows="True" 做到了。作为一个正在学习这个的人,你对我有什么建议,以便我自己找到这样的 UI 问题的答案?我不知道从哪里开始寻找,因为有太多我不知道的设置/属性。
  • 我建议您关注官方document 并且那里还有示例代码。如果您有任何问题,请随时在这里提问:)。
  • 您能否接受这个答案(点击这个答案左上角的☑️),以便我们可以帮助更多有同样问题的人:)。
猜你喜欢
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2022-10-07
  • 2015-02-06
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2014-07-16
相关资源
最近更新 更多