【发布时间】:2022-11-19 10:44:12
【问题描述】:
我正在尝试实现 CarouselView 以在我的应用程序上显示一组图像。显然,无论我尝试什么组合,我都无法删除 CarouselView 底部的空白。
我已经为CarouselView[红色]、Grid[绿色](在 DataTemplate 内)和IndicatorView[蓝色] 放置了BackgroundColor 属性,以查看尽管缺少StackLayout 并且似乎是 CarouselView 或 Grid 导致了不良行为。
这是我的 XAML 代码,ViewModel 上几乎没有任何内容,只有一个用于图像集合的模拟数据库:
<ContentPage.Content>
<Grid ColumnDefinitions="*"
RowDefinitions="Auto,Auto,Auto,1*">
<Label Grid.Row="0" Grid.Column="0"
Text="CarouselView Test"
TextColor="Black"
FontAttributes="Bold"
FontSize="20"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
Padding="10" />
<CarouselView Grid.Row="1" Grid.Column="0" BackgroundColor="Red" HeightRequest="{Binding ScreenWidth}"
x:Name="TheCarousel"
ItemsSource="{Binding ImageSourceCollection}"
IndicatorView="indicatorView">
<CarouselView.ItemTemplate>
<DataTemplate>
<Grid RowDefinitions="Auto" ColumnDefinitions="Auto" BackgroundColor="Green" HorizontalOptions="Center" VerticalOptions="Center">
<Image Grid.Row="0" Grid.Column="0" Source="{Binding .}" />
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<IndicatorView Grid.Row="2" Grid.Column="0" BackgroundColor="Blue"
x:Name="indicatorView"
IndicatorColor="LightGray"
IndicatorSize="10"
SelectedIndicatorColor="Black" />
</Grid>
</ContentPage.Content>
这是我当前构建的屏幕截图,其中 CarouselView/Grid 占用了大部分屏幕空间:
【问题讨论】:
-
您需要指定一些具体的高度值,否则旋转木马和网格将占用所有可用空间。因为轮播包含一个在运行时动态加载的图像,所以在计算布局时它不知道需要多大。