首页 > 解决方案 > Xamarin Forms:图像网格在元素行之间有太多空间

问题描述

我有一个包含 2 列的网格,用于托管图像。

<ScrollView VerticalOptions="Start">
        <Grid
          x:Name="gridLayout"
          RowSpacing="5"
          VerticalOptions="Start">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        </Grid>
    </ScrollView>

图像是以编程方式添加的,因为我不知道执行之前的数量。像这样声明图像

Image image = new Image
                {
                    Source = ImageSource.FromUri(new Uri(filepath)),
                    Aspect = Aspect.AspectFit,
                    BackgroundColor = Color.Red,
                    VerticalOptions = LayoutOptions.Start
                };

结果是这样的:

截屏

我的目的是缩小图像之间的垂直空间。

标签: c#.netxamlxamarinxamarin.forms

解决方案


额外的垂直间距不是由于Grid's RowSpacing,而是由于图像引入的填充AspectRatio。一旦Row'sHeightColumn' 的宽度设置为与Image' 的纵横比匹配并且AspectRatioImage 上的 Property 设置为Aspect.Fill额外的填充应该消失


推荐阅读