【问题标题】:Xamarin.Forms Page BackgroundImage propertyXamarin.Forms 页面 BackgroundImage 属性
【发布时间】:2016-10-19 03:07:26
【问题描述】:

您应该如何设置页面的背景图像,因为 BackgroundImage 是一个字符串?如有任何建议,我将不胜感激。

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

MainPage = new ContentPage 
{
    BackgroundImage = "Images/image.png"
}

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

【问题讨论】:

  • 图片必须在平台特定的文件夹中,例如Resources/drawable for android and Resources for iOS
  • 我将图片添加到Resources/drawable 文件夹(现在在Android 上测试),但它仍然无法正常工作...
  • 我已经这样做了
  • 你的设备分辨率是多少?如果它的分辨率更高,则将图像添加到每个 drawable-hdpi、drawable-xhdpi、drawable-xxhdpi、
  • 它工作正常,我从一个角度看设备,背景很暗,所以我没注意到,哈哈,谢谢!

标签: xamarin xamarin.forms


【解决方案1】:

如果我没记错的话,你不能共享资源。您必须将图像放在特定于平台的文件夹中,而不是使用 BackgroundImage = "image.png" 而不使用 Images/

编辑:

看来我有一部分是错误的。

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

【讨论】:

【解决方案2】:

如果您需要一个允许您更改 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>

【讨论】:

    【解决方案3】:

    在页面上设置图像:

    <Image Source="bg"></Image>
    

    现在您需要在每个平台上添加您的图像:

    iOS

    Test.iOS > Resources > bg.png
    

    安卓

    Test.Droid > Resources > bg.png
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-04
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 2020-04-24
      相关资源
      最近更新 更多