ListView中有比我需要的空间更多的空间

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

  

我该如何解决? 我看到人们使用HasUnevenRows="True"但它对我不起作用。

首先,Xamarin建议不要在ScrollView放置ListView ,因为它们都提供滚动和相互争斗。 我个人试过这个,它经常会导致奇怪的行为,特别是在Android上。

相反,我所做的是使用带有ListView.HeaderTemplate和带有ListView.FooterTemplate 。 Xamarin链接在这里这允许你的确切场景,但没有奇怪的行为问题。 如果您想看一个例子,请告诉我。

如果你必须必须使用当前的布局,你可以尝试查看这篇文章,讨论如何使用特定的HeightWidth或使用LayoutOptions来调整ListView大小,因为它不应该符合它的内容大小。 该post中有几个解决方案,我还没试过。

在这篇文章中给出了一个解决方法,即将ListView添加到StackLayout并将其添加到另一个StackLayout 。 然后将内部StackLayoutVerticalOptionsLayoutOptions.FillAndExpand

所以它可能看起来像这样:

   ....          ....   

正如hvaughan3所说,这真的不是一个好习惯。 但我有相同的情况,我使用行为的解决方法:

 public class ListViewHeightBehavior : Behavior { private ListView _listView; public static readonly BindableProperty ExtraSpaceProperty = BindableProperty.Create(nameof(ExtraSpace), typeof(double), typeof(ListViewHeightBehavior), 0d); public double ExtraSpace { get { return (double)GetValue(ExtraSpaceProperty); } set { SetValue(ExtraSpaceProperty, value); } } protected override void OnAttachedTo(ListView bindable) { base.OnAttachedTo(bindable); _listView = bindable; _listView.PropertyChanged += (s, args) => { var count = _listView.ItemsSource?.Count(); if (args.PropertyName == nameof(_listView.ItemsSource) && count.HasValue && count.Value > 0) { _listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace; } }; } } 

XAML:

         // Your template     

谢谢你们,但没有任何对我有用。 我自己认为的解决方案肯定不是最好的解决方案,但它对我有用…..

在XAML,我留下了一个空的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个)。 我将这个标记为帮助其他人解决此问题的正确答案。

但请, 如果这是一个无人能做到的大解决方案,请回答我,我会找到另一种方法来做到这一点