如何在 Xamarin 的 stackLayout 中全屏填充图像?

how to Fill Image fullscreen in stackLayout in Xamarin?

*如何在 Xamarin 的 stackLayout 中全屏填充图像? 我无法将 Image 设置为适合 stacklayout .XAML 文件代码

  <StackLayout Padding="0" BackgroundColor="Yellow">
    <Image Source="ic_splash.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" > </Image>
  </StackLayout>
</ContentPage>

输出屏幕截图 Android 和 Window Phone... 我想让图片适合背景。*

因为您的 android 模拟器上没有物理密钥。 使用带有物理密钥的模拟器会很好

最后我用下面的代码解决了问题....

<?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="Your.Namespace.Views.LoginView"
             Title="{Binding Title}"
             BackgroundImage="BackgroundImage.png">
  <StackLayout>
    <!-- Your stuff goes here -->
  </StackLayout>
</ContentPage>

我已经使用上面的代码将背景图像设置为适合屏幕....

BackgroundImage="BackgroundImage.png"

或者使用您的第一个代码,您可以将图像 属性 纵横比设置为 AspectFill,这最终会裁剪您的图像,同时保持纵横比。

<StackLayout Padding="0" BackgroundColor="Yellow">
<Image Source="ic_splash.png" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" > </Image>

BackgroundImage 属性 的问题是图像比例可能会随着屏幕尺寸的变化而变化。

Here 我找到了如何使用 RelativeLayout:

填充所有屏幕并保存比例
<RelativeLayout>
  <Image Source="Jupiter.png" Opacity="0.3"
              RelativeLayout.WidthConstraint=
                "{ConstraintExpression Type=RelativeToParent, Property=Width}"
              RelativeLayout.HeightConstraint=
                "{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
  <Grid RelativeLayout.WidthConstraint=
            "{ConstraintExpression Type=RelativeToParent, Property=Width}"
          RelativeLayout.HeightConstraint=
            "{ConstraintExpression Type=RelativeToParent, Property=Height}">

    <Label Text="Hello world from XAML"/>
  </Grid>
</RelativeLayout>