首页 > 解决方案 > Xamarin iOS 图像在 Grid 内重叠

问题描述

嘿嘿,

因此,在 Xamarin 中,我有一个<Grid>带有 an<Image>和几个<Label>s 的内部,都包裹在一个<ViewCell>. 这在 Xamarin.Android 中看起来非常好,但是在 Xamarin.iOS 中,图像与标签重叠。我不确定可能有什么区别 - 为什么它在 Xamarin.Android 中看起来不错,但在 iOS 中却很不稳定?

下面是我的 XAML 和几个模型来说明我的意思。

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <ViewCell.View>
            <Grid>
                <Grid.ColumnDefinitions>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Image Grid.Column="0" Grid.Row="0" Aspect="AspectFill" Source="{Binding ImageOverlayEN}" />
                <Label Grid.Column="0" Grid.Row="1" VerticalTextAlignment="Start" LineBreakMode="WordWrap" Text="{Binding DynamicOfferText}" FontSize="18">
                    <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                        <OnPlatform.iOS>RobotoCondensed-Regular</OnPlatform.iOS>
                        <OnPlatform.Android>RobotoCondensed-Regular.ttf#RobotoCondensed-Regular</OnPlatform.Android>
                        <OnPlatform.WinPhone>RobotoCondensed-Regular.ttf#RobotoCondensed</OnPlatform.WinPhone>
                    </OnPlatform>
                    </Label.FontFamily>
                </Label>
                <Label Grid.Column="0" Grid.Row="2" VerticalTextAlignment="Start" LineBreakMode="WordWrap" Text="{Binding DynamicOfferDetail}" FontSize="16">
                    <Label.FontFamily>
                        <OnPlatform x:TypeArguments="x:String">
                        <OnPlatform.iOS>RobotoCondensed-Regular</OnPlatform.iOS>
                        <OnPlatform.Android>RobotoCondensed-Regular.ttf#RobotoCondensed-Regular</OnPlatform.Android>
                        <OnPlatform.WinPhone>RobotoCondensed-Regular.ttf#RobotoCondensed</OnPlatform.WinPhone>
                    </OnPlatform>
                    </Label.FontFamily>
                </Label>
                </Grid>
            </ViewCell.View>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

我尝试设置HeightRequest但似乎没有什么不同:

#if __IOS__
    if (viewModel.Items.Count > 0)
    {
        MyListView.HeightRequest = 300 * viewModel.Items.Count;
    }
#endif

这是正在发生的事情的直观表示:

在此处输入图像描述

标签: xamarinxamarin.formsxamarin.iosxamarin.android

解决方案


想通了-与我RowDefinitions的一切准备有关Auto

这让它看起来恰到好处:

<RowDefinition Height="1*" />
<RowDefinition Height="0.18*" />
<RowDefinition Height="0.77*" />

我认为发生的事情是ListView首先渲染,行定义高度在图像加载完成之前Auto设置每行的高度,然后图像完成加载并与列表视图项目的其余部分(包括标签)重叠。这可能只是 iOS 的一个怪癖,因为 Android 在图像加载完成后计算行高(解释为什么它在 Android 中看起来不错)


推荐阅读