Xamarin.Forms:StackLayout 中的 ListView:如何设置高度? [英] Xamarin.Forms: ListView inside StackLayout: How to set height?

查看:33
本文介绍了Xamarin.Forms:StackLayout 中的 ListView:如何设置高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ContentPage 中,我在 ScrollView 内的 StackLayout 中有一个 ListView.当 OnAppearing 被调用时,ListView 被填充(ItemSource 被设置)在 ContentPage 中,我可以看到列表填充在模拟器中.StackLayout 的方向是 Vertical,在 ListView 下方我有一个 Button.

In a ContentPage I have a ListView inside a StackLayout inside a ScrollView. The ListView is populated (ItemSource is set) in the ContentPage when OnAppearing gets called and I can see that the list is populated in the emulator. The StackLayouts orientation is Vertical and below the ListView I have a Button.

我的问题是,无论 ListView 有多少个元素,它的高度都是 53.33.我希望 ListView 的高度与其中项目的总高度相匹配.通过设置 HeightRequest 我可以将 ListView 的高度设置为我想要的任何东西,但是因为我不知道 ListView 中项目的高度> 结果通常是与其下方按钮的距离不正确,因此看起来很难看.我试图将 ListViewStackLayout 上的 VerticalOptions 设置为 Start 和其他设置,但这确实不改变 53.33 的高度(如果我尝试结合使用 HeightRequestStart ,结果是 HeightRequest 胜出).

My problem is that no matter how many elements the ListView has, it gets the height of 53.33. I would like the height of the ListView to match the total height of the items in it. By setting HeightRequest I can set the height of the ListView to anything I want, but since I do not know the height of the items inside the ListView the result is most often that the distance to the button below it is incorrect and therefore looks ugly. I have tried to set VerticalOptions on both the ListView and the StackLayout to Startand other settings, but this does not change the height from 53.33 (and if I try to combine using HeightRequest and Start it turns out that HeightRequest wins out).

我该如何解决这个问题?

How can I solve this?

(请原谅来自 Xamarin 论坛的交叉发帖)

推荐答案

在我的例子中的解决方案是将 ListView 放在 StackLayout 中,然后把那个 >StackLayout 在主 StackLayout 中.然后我可以在内部 StackLayout(包含 ListView 的那个)上设置 VerticalOptions = LayoutOptions.FillAndExpand,结果是 ListView 获得了它需要的空间(当然这取决于数据).

The solution in my case was to put the ListView inside a StackLayout and then put that StackLayout inside the main StackLayout. Then I could set the VerticalOptions = LayoutOptions.FillAndExpand on the inner StackLayout (the one containing the ListView) with the result that the ListView got the space it needed (which of course varies depending on the data).

主要代码如下:

listView.ItemsSource = alternativeCells;
listView.ItemSelected += ListViewOnItemSelected;
var listStackLayout = new StackLayout
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    Orientation = StackOrientation.Vertical
};
listStackLayout.Children.Add(listView);
_stackLayout.Children.Add(listStackLayout);

如您所见,我添加了一个新的 StackLayout,其唯一目的是将 ListView 放入其中.然后我把 listStackLayout 放在主 _stackLayout 中.

As you see, I added a new StackLayout with the only purpose of putting the ListView inside it. Then I put that listStackLayout inside the main _stackLayout.

有关详细信息,请参阅此 Xamarin 论坛帖子上的帖子

See the post on this Xamarin forum post for more information

这篇关于Xamarin.Forms:StackLayout 中的 ListView:如何设置高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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