[英]Xamarin.Forms: adjust Image size to the width of the parent's in a BindableLayout
I'm trying to adjust the the Image
size to the width of the parent's container in a BindableLayout
, but I didn't found a way to achieve this.我正在尝试将Image
大小调整为BindableLayout
父容器的宽度,但我没有找到实现此目的的方法。
I'm based on this other topic to achieve this.我基于这个其他主题来实现这一目标。
I first tried to embed the Image
in a Grid
container like this:我首先尝试将Image
嵌入到Grid
容器中,如下所示:
<StackLayout x:Name="NewsList"
BindableLayout.ItemsSource="{Binding News}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Frame>
<Grid Padding="0" Margin="0"
VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
BackgroundColor="Red">
<Image Source="{Binding Image}" Aspect="AspectFit"/>
</Grid>
<Label Text="Description" />
</Frame>
</DataTemplate>
</BindableLayout>
</StackLayout
=> but we can see that I get red stripes around the images (at the top/bottom, or at the left/right): => 但我们可以看到图像周围出现红色条纹(在顶部/底部,或在左侧/右侧):
Then, I've tried to use a CachedImage
from FFImageLoading
like this:然后,我尝试使用CachedImage
的FFImageLoading
如下所示:
<Grid Padding="0" Margin="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
BackgroundColor="Orange">
<ffimageloading:CachedImage Source="{Binding Image}"
HorizontalOptions="Fill"
VerticalOptions="Fill"
Aspect="AspectFill"
DownsampleToViewSize="True"/>
</Grid>
=> but in this case, the images are not fully visible, or are truncated => 但在这种情况下,图像不完全可见,或被截断
=> So is there another way allowing me to display an image in the full width of the parent's container, and keeping the original ratio? => 那么有没有另一种方法可以让我以父容器的全宽显示图像,并保持原始比例?
Update 1:更新 1:
The displayed images are coming from Facebook posts.显示的图像来自 Facebook 帖子。 Here are the fourth images that are displayed:以下是显示的第四张图片:
Update 2:更新 2:
I've tried the solution given by @Leo:我已经尝试过@Leo 给出的解决方案:
<StackLayout Orientation="Vertical" x:DataType="model:News">
<Image Source="{Binding Image}" Aspect="AspectFill"/>
<Label Text="Description" />
</StackLayout>
But the result is still not good as the first and fourth images are truncated:但是结果仍然不好,因为第一张和第四张图像被截断了:
I download the picture and do like below,you could check it:我下载了图片,然后像下面一样,你可以检查一下:
<StackLayout x:Name="NewsList" BackgroundColor="Red"
BindableLayout.ItemsSource="{Binding News}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical">
<Image Source="{Binding .}" Aspect="AspectFill"/>
<Label Text="Description" />
</StackLayout>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
the effect like:效果如下:
I finally found a solution that worked for mehere , it's the solution given by Frankvans :我终于在这里找到了一个对我有用的解决方案,这是Frankvans给出的解决方案:
class ImageFit : Image {
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint) {
SizeRequest sizeRequest = base.OnMeasure(double.PositiveInfinity, double.PositiveInfinity);
var innerRatio = sizeRequest.Request.Width / sizeRequest.Request.Height;
if (double.IsNaN(innerRatio))
return sizeRequest;
// Width needs to be adjusted
if (double.IsInfinity(heightConstraint)) {
// Height needs to be adjusted
if (double.IsInfinity(widthConstraint)) {
widthConstraint = sizeRequest.Request.Width;
heightConstraint = sizeRequest.Request.Height;
} else {
// Adjust height
heightConstraint = widthConstraint * sizeRequest.Request.Height / sizeRequest.Request.Width;
}
} else if (double.IsInfinity(widthConstraint)) {
// Adjust width
widthConstraint = heightConstraint * sizeRequest.Request.Width / sizeRequest.Request.Height;
} else {
// strech the image to make it fit while conserving it's ratio
var outerRatio = widthConstraint / heightConstraint;
var ratioFactor = (innerRatio >= outerRatio) ?
(widthConstraint / sizeRequest.Request.Width) :
(heightConstraint / sizeRequest.Request.Height);
widthConstraint = sizeRequest.Request.Width * ratioFactor;
heightConstraint = sizeRequest.Request.Height * ratioFactor;
}
sizeRequest = new SizeRequest(new Size(widthConstraint, heightConstraint));
return sizeRequest;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.