ListView 中的空间比我需要的多 [英] There is more space than I need in ListView
问题描述
我使用 StackLayout 和 ListView 来显示视图的某些部分,但 ListView 占用的空间超出了我的需要,并且在列表的最后一行和配置文件延续之间留有空白.看起来我的 ListView 的行数比实际列表的长度多,或者它有一个固定的高度,我不知道......这是我的 XAML:
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>
我该如何解决?我看到有人使用 HasUnevenRows="True"
但它对我不起作用.
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.....
在 XAML 中,我留下了一个空的 StackLayout,如下所示:
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>
然后,在.cs文件中,我在InitializeComponent()方法之后调用了这个方法:
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);
}
}
我这样做是因为列表并不是真正的 ListView,因为我不需要直接在列表中滚动功能,它只是视图的另一部分.我认为它不会导致任何性能问题,因为它将具有少量项目(最多 5 个).我将此标记为正确答案,以帮助其他人解决此问题.
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屋!