Xamarin.Forms页面BackgroundImage属性

mkk*_*nen 15 xamarin xamarin.forms

你应该如何设置页面的背景图像,因为BackgroundImage是一个字符串?我非常感谢任何建议.

到目前为止,我已经尝试过:

MainPage = new ContentPage 
{
    BackgroundImage = "Images/image.png"
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.图像文件位于PCL项目中.

ars*_*ena 14

如果我没有弄错你就无法分享资源.你必须把图像中的特定平台的文件夹,并比使用BackgroundImage = "image.png"Images/

编辑:

看来我有些错误.

可以通过嵌入图像来共享图像,而不是为不同的平台创建多个副本:https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images

  • 您当然可以共享资源(https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images) (3认同)

jze*_*ino 9

如果您需要一个允许您更改AspectRatio并调整图像的解决方案,您可以使用:

XAML:

<?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="Pages.PhotoPage">
        <Grid >
            <Image Source="background.png" Aspect="AspectFit" />
            <!-- Place here the rest of the layout for the page. -->
        </Grid >
</ContentPage>
Run Code Online (Sandbox Code Playgroud)