英文:
CollectionView takes unexpected height on iOS
问题
在将Xamarin.iOS更新到最新版本后,我意识到将CollectionView
放入StackLayout
并设置VerticalOptions = FillAndExpand
会导致它占据无限的垂直空间,并最终排除在iOS和MacCatalyst上放置在其下的任何其他控件。
如果尝试删除FillAndExpand
选项,它将占据预期的空间,但不会滚动到末尾。
如何实现正确的行为并保持下方控件的定位?
英文:
After updating Xamarin.iOS to the latest version, I realized that putting a CollectionView
into a StackLayout
with VerticalOptions = FillAndExpand
would cause it to take endless vertical space and eventually rule out any other control that has been put below on iOS and MacCatalyst.
If I try to remove the FillAndExpand
option, it will take the expected space but it won't scroll to the end.
How can I achieve the correct behavior keeping the below controls positioning?
答案1
得分: 2
StackLayout的扩展选项FillAndExpand
在MAUI中已被弃用。对于所有其他*AndExpand
变体也是如此。虽然在Xamarin.Forms中很受欢迎,但在.NET MAUI中最终将不再起作用,因此应尽量避免使用。
另外,*AndExpand
选项从未适用于除StackLayout之外的任何其他布局。在Xamarin.Forms和.NET MAUI中,它们不会对Grid、RelativeLayout或AbsoluteLayout产生任何影响。
如果您需要使项目填充指定数量的空间,应该使用Grid,然后设置VerticalOptions="Fill"
:
<Grid
RowDefinitions="*"
HeightRequest="300">
<CollectionView
Grid.Row="0"
VerticalOptions="Fill"
ItemsSource="{Binding MyItems}" />
</Grid>
这将创建一个具有特定高度的Grid,并且有一个占据Grid所有可用空间的单个行。放置在Grid内的CollectionView将填充其父项提供的可用空间。
重要提示: 当Grid具有VerticalOptions="Fill"
而不是定义固定的HeightRequest
时,也可以正常工作,但请确保它不包含在可滚动视图(例如ScrollView)中,因为在那种情况下可用空间实际上是无限的。
英文:
The StackLayout expansion option FillAndExpand
is deprecated in MAUI. This is the case for all other *AndExpand
variants, as well. Popular in Xamarin.Forms, they are not going to work anymore eventually in .NET MAUI and thus should be avoided, if possible.
Also, the *AndExpand
options never applied to any other layout other than StackLayout. They don't have any effect in a Grid, RelativeLayout or AbsoluteLayout in Xamarin.Forms and .NET MAUI.
If you need an item to fill a defined amount of space, you should use a Grid and then set the VerticalOptions="Fill"
instead:
<Grid
RowDefinitions="*"
HeightRequest="300">
<CollectionView
Grid.Row="0"
VerticalOptions="Fill"
ItemsSource="{Binding MyItems}" />
</Grid>
This will create a Grid with a specific height and a single row which takes up all the available space of the Grid. The CollectionView placed inside the Grid will then fill the available space that its parent provides.
Important: This also works when the Grid has its VerticalOptions="Fill"
instead of defining a fixed HeightRequest
, however, make sure that this then is not contained inside a scrollable view such as a ScrollView, because in that case the available space is virtually unlimited.
答案2
得分: 0
对于CollectionView
要正确调整尺寸,以防止它占用过多垂直空间(最终排除下方的所有控件),在iOS上,它需要嵌套到一个Grid
中,其中VerticalOptions = FillAndExpand
。这将允许CollectionView
滚动到末尾,并允许下方的任何其他控件按预期显示在View
中。
英文:
For the CollectionView
to be dimensioned correctly and to prevent it from taking to much vertical space (and eventually rule out all the controls below) on iOS, it needs to be nested into a Grid
with VerticalOptions = FillAndExpand
. This will let the CollectionView
be scrolled to the end and will also allow any other control below to be shown as expected in the View
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论