0
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="StackStuff.MainPage">

    <StackLayout Orientation="Vertical">
        <Image Source="image.jpg" VerticalOptions="Fill" HorizontalOptions="Fill"/>
    </StackLayout>

</ContentPage>

I have a Xamarin.Forms project where I want an image to resize inside a StackLayout. I stripped it down to the minimal code above and if I resize the window, the image will only resize based off the window width. i.e. the image gets clipped off the bottom of the window if I change the height.

If I change the StackLayout's Orientation to Horizontal, I get the opposite - it'll resize based off a changing height, but not width.

If I completely remove the StackLayout and have only the image on the page, then the entire image will always be visible which is the behaviour I expected when it's inside the StackLayout.

Can anyone explain why this is? If I replace the Image with a BoxView, it'll happily resize and stay within the bounds of the StackLayout.

Thanks.

Grant J
  • 1,056
  • 3
  • 10
  • 18
  • Check Aspect property = uniformfill – Cfun Mar 23 '21 at 16:50
  • @Cfun There is no uniformfill for the Aspect property in Xamarin.Forms. the default is AspectFit: https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.aspect?view=xamarin-forms – Grant J Mar 23 '21 at 16:55
  • sorry I couldn't understand your problem better, but just as an suggestion - did you tried setting VerticalOptions & HorizontalOptions of image & stack layout to FillAndExpand? – Blu Mar 23 '21 at 17:18
  • @Blu, thanks for your suggestion. Yes, I've tried all of the Horizontal and Vertical options. I'm pretty sure I'm not doing anything wrong and it's a limitation of a StackLayout but it'd be interesting to know why it doesn't work. – Grant J Mar 23 '21 at 17:47
  • Perhaps you could try showing two screenshots to make your question more clear ? – Leo Zhu Mar 24 '21 at 08:40

1 Answers1

0

I solved this by wrapping the image in a grid:

<StackLayouenter code heret Orientation="Vertical">
    <Grid>        
        <Image Source="image.jpg" VerticalOptions="Fill" HorizontalOptions="Fill"/>
    </Grid>
</StackLayout>

I have no idea why it works but I guess that's Xamarin.Forms for you.

Grant J
  • 1,056
  • 3
  • 10
  • 18