Xamarin Forms 图像填充宽度与适当的方面 [英] Xamarin Forms Image fill width with proper aspect

查看:29
本文介绍了Xamarin Forms 图像填充宽度与适当的方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 xaml(堆栈布局中的图像).一切都是合乎逻辑的:我将 Aspect 设置为 AspectFit,将 Horizo​​ntalOptions 设置为 FillAndExpand.图片的宽度必须填满 stacklayout 的宽度.必须计算高度才能对 Aspect 成像.

Here is xaml (an image in stacklayout). Everything is logical: I set Aspect to AspectFit and HorizontalOptions to FillAndExpand. Width of image must fill the width of stacklayout. Height must be calculated in order to image Aspect.

<Image BackgroundColor="Fuchsia"
       Aspect="AspectFit"
       Source="{Binding GroupLogo}"
       HorizontalOptions="FillAndExpand"
       VerticalOptions="FillAndExpand"/>

它填充了宽度但它不想绘制全宽的图像.为什么?

It fill the width but It doesn't want to draw image full width. Why?

推荐答案

AspectFit 做它在锡上所说的,它将整个图像放入视图中,这可能会留下部分视图空的.直接来自 Xamarin 的文档:

AspectFit does what it says on the tin, it will fit the whole image into the View, which may leave parts of the view empty. Straight from Xamarin's documentation:

缩放图像以适应视图.某些部分可能会留空(字母装箱).

Scale the image to fit the view. Some parts may be left empty (letter boxing).

您正在寻找的是 AspectFill,它将缩放图像以适合:

What you're looking for is AspectFill, which will scale the image to fit:

缩放图像以填充视图.某些部分可能会被裁剪以填充视图.

Scale the image to fill the view. Some parts may be clipped in order to fill the view.

如果您想让图像视图成为图像的高度,我建议将 HeightRequest 添加到图像的高度.根据我的经验,图像控件似乎不会在 Xamarin 中自动缩放,因为默认情况下原生控件似乎也不支持.

If you are trying to get the image view to be the height of the image, I'd suggest adding a HeightRequest to the height of the image. Image controls don't seem to automatically scale in Xamarin, in my experience, as the native controls don't appear to support that by default either.

参考

这篇关于Xamarin Forms 图像填充宽度与适当的方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆