简体   繁体   English

Xamarin.Forms:在 BindableLayout 中将图像大小调整为父级的宽度

[英]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:然后,我尝试使用CachedImageFFImageLoading如下所示:

<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:以下是显示的第四张图片:

  1. https://scontent-cdt1-1.xx.fbcdn.net/v/t1.0-9/s720x720/118713552_3623822074306674_9076571821151026884_n.jpg?_nc_cat=110&_nc_sid=110474&_nc_ohc=8xcbXj0tf5IAX-b_jE9&_nc_ht=scontent-cdt1-1.xx&tp=7&oh=60af4b756944856e28327736fd542c65&oe=5F98D58D https://scontent-cdt1-1.xx.fbcdn.net/v/t1.0-9/s720x720/118713552_3623822074306674_9076571821151026884_n.jpg?_nc_cat=110&_nc_sid=110474&_nc_ohc=8xcbXj0tf5IAX-b_jE9&_nc_ht=scontent-cdt1-1.xx&tp=7&oh= 60af4b756944856e28327736fd542c65&oe=5F98D58D
  2. https://scontent-cdg2-1.xx.fbcdn.net/v/t15.13418-10/96415113_248439429597332_6367448024599232512_n.jpg?_nc_cat=100&_nc_sid=ad6a45&_nc_ohc=2jBGHupMA20AX-8Y-M8&_nc_ht=scontent-cdg2-1.xx&oh=918470153ca23c927b921b2f1a61d4a5&oe=5F97D9C7 https://scontent-cdg2-1.xx.fbcdn.net/v/t15.13418-10/96415113_248439429597332_6367448024599232512_n.jpg?_nc_cat=100&_nc_sid=ad6a45&_nc_ohc=2jBGHupMA20AX-8Y-M8&_nc_ht=scontent-cdg2-1.xx&oh=918470153ca23c927b921b2f1a61d4a5&oe= 5F97D9C7
  3. https://scontent-cdg2-1.xx.fbcdn.net/v/t15.5256-10/95397765_284933452664428_1743250590344937472_n.jpg?_nc_cat=111&_nc_sid=ad6a45&_nc_ohc=LWajKszkHiMAX-sqkeZ&_nc_oc=AQmqjn5ykjy2tYfmRvpQChhtdsJZgk9_Smn3oU9weGt5xe9QY4933u_sNV1qAnZrIFc&_nc_ht=scontent-cdg2-1.xx&oh=c93af2de18eb417b224d8ea211197cda&oe=5F98A096 https://scontent-cdg2-1.xx.fbcdn.net/v/t15.5256-10/95397765_284933452664428_1743250590344937472_n.jpg?_nc_cat=111&_nc_sid=ad6a45&_nc_ohc=LWajKszkHiMAX-sqkeZ&_nc_oc=AQmqjn5ykjy2tYfmRvpQChhtdsJZgk9_Smn3oU9weGt5xe9QY4933u_sNV1qAnZrIFc&_nc_ht=scontent-cdg2-1.xx&oh=c93af2de18eb417b224d8ea211197cda&oe= 5F98A096
  4. https://scontent-cdg2-1.xx.fbcdn.net/v/t1.0-9/s720x720/95921743_3263337923688426_3234683279575613440_o.jpg?_nc_cat=102&_nc_sid=8024bb&_nc_ohc=AkRxtEdIyU8AX99QWmR&_nc_ht=scontent-cdg2-1.xx&tp=7&oh=6dc746bd0ea30097aee78f57a6395fb4&oe=5F980B69 https://scontent-cdg2-1.xx.fbcdn.net/v/t1.0-9/s720x720/95921743_3263337923688426_3234683279575613440_o.jpg?_nc_cat=102&_nc_sid=8024bb&_nc_ohc=AkRxtEdIyU8AX99QWmR&_nc_ht=scontent-cdg2-1.xx&tp=7&oh=6dc746bd0ea30097aee78f57a6395fb4&oe= 5F980B69

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM