【问题标题】:Xamarin: Label disappears inside ListViewXamarin:标签在 ListView 中消失
【发布时间】:2020-11-14 12:09:27
【问题描述】:

正在为 Android 设备制作项目。

我已经搜索了一段时间,但找不到为什么会发生这种情况的答案。

<ListView x:Name="ScorebdList"
          Grid.Row="0"
          Grid.ColumnSpan="3"
          Grid.RowSpan="4"
          SelectionMode="None"
          Header="HIGHSCORES">
    <ListView.ItemTemplate>
        <DataTemplate >
            <ViewCell>
                <ViewCell.View>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="50" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" />
                        </Grid.ColumnDefinitions>
                            <Label Text="{Binding Score}" HeightRequest="50" 
                                   MinimumHeightRequest="50" WidthRequest="50" 
                                   MinimumWidthRequest="50" HorizontalTextAlignment="Center" 
                                   BackgroundColor="Red"/>
                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

说明:
在这里,我试图设置标签的 height 和 width 和 Grid 的相同。 标签上的红色背景以查看它的去向。

根本没有显示标签

如果我这样做:

<ListView x:Name="ScorebdList"
          Grid.Row="0"
          Grid.ColumnSpan="3"
          Grid.RowSpan="4"
          SelectionMode="None"
          Header="HIGHSCORES"
          HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate >
            <ViewCell>
                <ViewCell.View>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                            <Label Text="{Binding Score}" HorizontalTextAlignment="Center" 
                                   BackgroundColor="Red"/>
                    </Grid>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我添加了 HasUnevenRows 属性并将网格中的大小和宽度设置为自动。还删除了 Label 的高度/宽度请求。

现在标签会显示,但不是我想要的那样。

我测试的其他内容:
我还尝试将标签设置在 &lt;StackLayout&gt; 而不是 Grid 内,结果相同。
(在 &lt;ViewCell&gt; 内)
还尝试使用&lt;TextCell&gt; 而不是&lt;ViewCell&gt;,我无法居中但固定高度和宽度...

问题是:
为什么?我错过了什么?

使用第二个代码示例并不是我想要的样子。 我希望我的&lt;ListView&gt; 以我设置的高度和宽度在视图的中心显示高分。

(下面的代码与上面的代码颜色不同,标签为#D87040(橙色),网格背景为#321F1F(深红色)”) 这是两张图片,第一张图片的宽度/高度为 '*'。

这是我在 w/h 上将定义设置为 50 的第二张图片。

【问题讨论】:

  • 尝试设置 Grid 的高度值,而不是 Label。和/或设置 ListView 的 RowHeight
  • 都试过了。问题是,当我这样做时,行得到了想要的正确高度,但标签完全消失了......并且没有显示分数。

标签: listview xamarin xamarin.forms xamarin.android


【解决方案1】:

您的ListView 没有ItemSource 绑定

【讨论】:

    【解决方案2】:

    使用转换器通过 Listview RowHeight 更改网格行高。

    转换器:

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }     
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
       
    }
    

    Xaml:

      <ContentPage.Resources>
        <ResourceDictionary>
            <local:MyConverter x:Key="MyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <ListView
            x:Name="ScorebdList"
            Grid.Row="0"
            Grid.RowSpan="4"
            Grid.ColumnSpan="3"
            Header="HIGHSCORES"
            RowHeight="100"
            SelectionMode="None"
            HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="{Binding Source={x:Reference ScorebdList}, Path=RowHeight, Converter={StaticResource MyConverter}}" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="100" />
                                </Grid.ColumnDefinitions>
                                <Label
                                    BackgroundColor="Red"
                                    HorizontalTextAlignment="Center"
                                    Text="{Binding score}" />
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
    

    代码背后:

     public partial class Page17 : ContentPage
    {
        public ObservableCollection<Score> scores { get; set; }
        public Page17()
        {
            InitializeComponent();
            scores = new ObservableCollection<Score>()
            {
                new Score{ score="Score1"},
                new Score{ score="Score2"},
                new Score{ score="Score3"},
    
            };
            ScorebdList.ItemsSource = scores;
            this.BindingContext = this;
        }
    }
    public class Score
    {
        public string score { get; set; }
    }
    

    【讨论】:

    • 感谢代码能达到它的目的,但不能解决我的问题。我可以调整列的高度和宽度,但是当我这样做时标签消失了。
    • 对于我提供的代码,更改了列高和列宽,但标签仍然存在。您能否提供有关失踪的更多细节?
    • 只要我将高度或宽度设置为定义,标签就会消失。所以你的代码确实有效,但没有为我显示标签。如果我在设置高度和宽度的情况下使用您的代码,则标签会显示。感觉好像我在这里错过了一些简单的东西......
    【解决方案3】:

    从您的代码中,我看到您从未将 Grid.ColumnGrid.Row 设置为您的标签

    <Label Text="{Binding Score}" HorizontalTextAlignment="Center" 
        BackgroundColor="Red" Grid.Column="0" Grid.Row="0"/>
    

    【讨论】:

    • 谢谢,但这不是必需的。但是尝试了相同的结果。
    • 从您的第一张图片的代码中,我猜您需要将ColumnSpacing="0"RowSpacing="0" 设置为您的网格
    • 是的,这是个好主意!你真的帮我解决了!出于某种原因,设置间距什么也没做,很奇怪。但相反,我使用了 Margin="-50" 并且确实有效。
    • 很高兴听到这个消息
    【解决方案4】:

    这个问题真的很奇怪,但是非常感谢所有帮助我的人!

    最后我使用Margin="-50" 来设置网格。 仍然很奇怪我不能使用高度请求、行/列间距……甚至更多。

    不过暂时解决了,再次感谢!

    Margin="-50 0"

    【讨论】:

    • 感谢分享,别忘了采纳答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    相关资源
    最近更新 更多