Xamarin的.表格CollectionView GridItemsLayout相同高度元素

2022-01-27 20:34:19 标签 iosxamarin.formscollectionview

我如何使CollectionView渲染所有项目具有相同的高度,而使用GridItemsLayout在iOS上?它已经在Android上工作,一行中的所有元素都得到相同的高度,但在iOS上,它们是在行中心垂直挤压。

这是它目前在iOS上的表现:

这是它目前在Android上的样子(它在iOS上也应该是这样的——所有元素都在同一行中缩放到相同的高度):

到目前为止,我的XAML看起来是这样的:

<CollectionView Grid.Row="1" x:Name="DataListView" ItemSizingStrategy="MeasureAllItems" ItemsSource="{Binding Data}" IsGrouped="False" ItemTemplate="{StaticResource EntryTemplate}"
                Header="{Binding Header}" Footer="{Binding Footer}" BackgroundColor="{DynamicResource DarkBackgroundColor}">
    <CollectionView.ItemsLayout>
        <!-- Span is set in OnSizeAllocated to make this "dynamic" -->
        <GridItemsLayout x:Name="GridItemsLayout" Orientation="Vertical" Span="1" />
    </CollectionView.ItemsLayout>
    <CollectionView.HeaderTemplate>
        <DataTemplate x:DataType="x:String">
            <Grid Padding="10,20">
                <Label Text="{Binding}" TextColor="{DynamicResource TextColor}" FontAttributes="Bold" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" />
            </Grid>
        </DataTemplate>
    </CollectionView.HeaderTemplate>
    <CollectionView.FooterTemplate>
        <DataTemplate x:DataType="x:String">
            <Grid Padding="{OnPlatform iOS=20 30 20 20, Default=20}">
                <Label Text="{Binding}" TextColor="{DynamicResource LightTextColor}" FontSize="Micro" HorizontalTextAlignment="Center" LineBreakMode="WordWrap" />
            </Grid>
        </DataTemplate>
    </CollectionView.FooterTemplate>
</CollectionView>

这是我的输入数据模板XAML:

<DataTemplate x:Key="EntryTemplate" x:DataType="data:Entry">
    <Grid BackgroundColor="Red" VerticalOptions="FillAndExpand" Margin="0">
        <Frame WidthRequest="{Binding Converter={StaticResource FlowColumnWidthConverter}, ConverterParameter=324}" BackgroundColor="{DynamicResource DarkerBackgroundColor}"
                BorderColor="{Binding IsActive, Converter={StaticResource ActiveToColorConverter}}" HorizontalOptions="Center" Margin="8" Padding="10" CornerRadius="5">
            <Frame.GestureRecognizers>
                <TapGestureRecognizer Tapped="OnChecklistElementTapped" />
            </Frame.GestureRecognizers>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="40" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Image Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Source="{Binding Type, Converter={StaticResource TypeToEmbeddedImageConverter}}" WidthRequest="32" HeightRequest="32" Margin="4" />
                <Label Grid.Row="0" Grid.Column="1" Text="{Binding Name}" FontAttributes="Bold" LineBreakMode="TailTruncation" VerticalOptions="Start" />
                <Label Grid.Row="1" Grid.Column="1" Text="{Binding Region}" FontSize="12" LineBreakMode="TailTruncation" VerticalOptions="Start" Margin="0,-6,0,0" />
                <Label Grid.Row="2" Grid.Column="1" Text="{Binding AdditionalData}" TextType="Html" FontSize="12" LineBreakMode="WordWrap" Margin="0"
                        IsVisible="{Binding AdditionalData, Converter={StaticResource EmptyToVisibilityConverter}}" InputTransparent="True" />
                <Label Grid.Row="3" Grid.Column="1" Text="{Binding Description}" FontSize="12" LineBreakMode="WordWrap"
                        IsVisible="{Binding Description, Converter={StaticResource EmptyToVisibilityConverter}}" />
                <Switch Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" IsToggled="{Binding IsDone}" HorizontalOptions="Center" VerticalOptions="Center" />
                <StackLayout Grid.Row="2" Grid.Column="2" Grid.RowSpan="2" Orientation="Horizontal" Spacing="12" HorizontalOptions="Center" VerticalOptions="Start" Margin="0,4,0,4">
                    <controls:TintedImageButton Source="Details.png" Clicked="OnNavigateToDetailsButtonClicked" HeightRequest="32" WidthRequest="32"
                                                IsVisible="{Binding Details, Converter={StaticResource EmptyToVisibilityConverter}}"
                                                HorizontalOptions="Center" BackgroundColor="Transparent" />
                    <controls:TintedImageButton Source="Map.png" Clicked="OnNavigateToMapButtonClicked" HeightRequest="32" WidthRequest="32"
                                                HorizontalOptions="Center" BackgroundColor="Transparent" IsVisible="{x:Static resources:Metadata.MAP_ENABLED}" />
                </StackLayout>
            </Grid>
        </Frame>
    </Grid>
</DataTemplate>

这是我后面的代码(我设置有多少列应该是):

protected override void OnSizeAllocated(double width, double height)
{
    base.OnSizeAllocated(width, height);
    GridItemsLayout.Span = Convert.ToInt32(Math.Floor(width / 340d));
}

有人知道为什么这在iOS上看起来不同吗?我应该如何解决这个问题?

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码