ListView中的空间超出了我的需要 [英] There is more space than I need in ListView

查看:96
本文介绍了ListView中的空间超出了我的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用StackLayout和ListView来显示视图的某些部分,但是ListView占用了比我所需更多的空间,并且在列表的最后一行和配置文件延续之间留有空白.看来我的ListView的行数多于实际列表的长度,或者它的高度是固定的,我不知道...这是我的XAML:

<ScrollView>
  <StackLayout Orientation="Vertical" Spacing="10">

    <Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.Category.Name,  StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/>

    <Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <ListView  ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
      <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

    <Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand"  />

    <Button Text="Ver no mapa"/>

  </StackLayout>
</ScrollView>

我该如何解决?我看到有人在使用HasUnevenRows="True",但对我却无效.

解决方案

谢谢你们,但没有任何帮助.我自己认为的解决方案当然不是最好的,但是对我有用.

在XAML上,我像这样留下了一个空的StackLayout:

    <StackLayout x:Name="ContactsList" Orientation="Vertical" Spacing="10" Padding="8,0">
      <!-- Empty because it will be filled in code mode -->
    </StackLayout>

然后,在.cs文件中,我在InitializeComponent()方法之后调用了此方法:

    private void setUpContacts(Establishment establishment)
    {
        foreach(Contact contact in establishment.Contacts)
        {
            StackLayout row = new StackLayout
            {
                Orientation = StackOrientation.Vertical
            };

            row.Children.Add(new Label
            {
                TextColor = Color.Gray,
                Text = string.Format("{0}:", contact.StringType)
            });

            row.Children.Add(new Label
            {
                TextColor = Color.Black,
                FontAttributes = FontAttributes.Bold,
                Text = contact.Value,
            });

            row.GestureRecognizers.Add(new TapGestureRecognizer {
                Command = new Command(() => OnContactTapped(row, contact))
            });

            ContactsList.Children.Add(row);
        }
    }

之所以这样做,是因为列表不是真正的ListView,因为我不需要直接在列表中滚动功能,所以它只是视图的另一部分.我认为它不会造成任何性能问题,因为它将包含少量项目(最多5个).我将此标记为可以帮助其他人解决此问题的正确答案.

请,如果这是一种不适用于其他人的解决方案,请告诉我,我将找到另一种方法来解决此问题.

I'm using StackLayout and ListView to show some part of a view, but the ListView takes more space than I need, and leaves a blank space between the last row of the list and the profile continuation. It seems my ListView has more lines than the real list's length, or it has a fixed Height, I don't know... Here's my XAML:

<ScrollView>
  <StackLayout Orientation="Vertical" Spacing="10">

    <Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.Category.Name,  StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/>

    <Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <ListView  ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
      <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

    <Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand"  FontAttributes="Bold"/>

    <Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand"  />

    <Button Text="Ver no mapa"/>

  </StackLayout>
</ScrollView>

How can I fix it? I saw people using HasUnevenRows="True" but it didn't work to me.

解决方案

Thank you guys, but nothing worked for me. The solution that I thought by myself surely isn't the best one, but it works for me.....

At the XAML I left an empty StackLayout like this:

    <StackLayout x:Name="ContactsList" Orientation="Vertical" Spacing="10" Padding="8,0">
      <!-- Empty because it will be filled in code mode -->
    </StackLayout>

And then, in the .cs file, I called this method after InitializeComponent() method:

    private void setUpContacts(Establishment establishment)
    {
        foreach(Contact contact in establishment.Contacts)
        {
            StackLayout row = new StackLayout
            {
                Orientation = StackOrientation.Vertical
            };

            row.Children.Add(new Label
            {
                TextColor = Color.Gray,
                Text = string.Format("{0}:", contact.StringType)
            });

            row.Children.Add(new Label
            {
                TextColor = Color.Black,
                FontAttributes = FontAttributes.Bold,
                Text = contact.Value,
            });

            row.GestureRecognizers.Add(new TapGestureRecognizer {
                Command = new Command(() => OnContactTapped(row, contact))
            });

            ContactsList.Children.Add(row);
        }
    }

I made this because the list isn't a really ListView, since I don't need scrolling functions directly in the list, it is just another part of the view. It won't cause any performance problem, I think, because it will have a small number of items (maximum 5). I'm marking this one as the correct answer to help others with this problem.

Please, if this is a solution that does not work for others, let me know and I'll find another way to do this.

这篇关于ListView中的空间超出了我的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆