Xamarin Forms Image 控件分配的高度超过请求的高度

我正在为我的元素设置一个显式HeightRequest的;但是调试的时候,实际好像跳到了. 这是我的 Xaml:16ImageHeight70

<Grid Margin="1" Padding="0" ColumnSpacing="0" RowSpacing="0" HeightRequest="17">

    <BoxView CornerRadius="3" Color="LightGray"/>

    <BoxView Margin="1" CornerRadius="3" BackgroundColor="White"/>

    <StackLayout Margin="2,0,2,0" Orientation="Horizontal" HeightRequest="17" Spacing="0">

        <Image Source="hooray_icon"

               WidthRequest="16"

               HeightRequest="16"

               Margin="1,0,1,0"

               Aspect="Fill"/>

        <Label Text="{Binding HoorayCount}"

               Margin="1,0,1,0"

               VerticalOptions="Center"

               VerticalTextAlignment="Center"

               HorizontalOptions="Center"

               HorizontalTextAlignment="Center"

               HeightRequest="16"/>

    </StackLayout>

</Grid>

预期设计:

http://img4.mukewang.com/638c0774000150d802030032.jpg

实际设计:

http://img1.mukewang.com/638c077c0001ac2d00590070.jpg

hooray_icon是一个 512x512 PNG 图像。

Xamarin.Forms 版本:3.6.0.293080

PS 我知道这是通过使用和覆盖以及调试和方法Image导致问题的元素。如果我弄错了,请纠正我。class CustomImage : ImageOnMeasureOnSizeAllocated


Cats萌萌
浏览 121回答 1
1回答

千巷猫影

回答HeightRequest不能保证,这只是一个请求。如果 Xamarin.Forms 能够满足请求,它会执行它,但根据屏幕大小和布局,它可能无法满足请求。解决方案设置Grid.ColumnDefinition'sWidth和Grid.RowDefinition'sHeight的值。代码<Grid Margin="1" Padding="0" ColumnSpacing="0" RowSpacing="0">&nbsp; <Grid.RowDefinitions>&nbsp; &nbsp; <RowDefinition Height="17" />&nbsp; </Grid.RowDefinitions>&nbsp; <Grid.ColumnDefinitions>&nbsp; &nbsp; <ColumnDefinition Width="17" />&nbsp; &nbsp; <ColumnDefinition Width="17" />&nbsp; &nbsp; <ColumnDefinition Width="17" />&nbsp; </Grid.ColumnDefinitions>&nbsp; &nbsp; <BoxView CornerRadius="3" Color="LightGray" Grid.Row="0" Grid.Column="0"/>&nbsp; &nbsp; <BoxView Margin="1" CornerRadius="3" BackgroundColor="White" Grid.Row="0" Grid.Column="1"/>&nbsp; &nbsp; <StackLayout Margin="2,0,2,0" Orientation="Horizontal" Spacing="0" Grid.Row="0" Grid.Column="2">&nbsp; &nbsp; &nbsp; &nbsp; <Image Source="hooray_icon"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Margin="1,0,1,0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Aspect="Fill"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HeightRequest="16"/>&nbsp; &nbsp; &nbsp; &nbsp; <Label Text="{Binding HoorayCount}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Margin="1,0,1,0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VerticalOptions="Center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VerticalTextAlignment="Center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HorizontalOptions="Center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HorizontalTextAlignment="Center"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HeightRequest="16"/>&nbsp; &nbsp; </StackLayout></Grid>
打开App,查看更多内容
随时随地看视频慕课网APP