Xamarin.Forms Xaml背景图片

12

我刚刚开始使用Xamarin.Forms应用程序,我想在我的XAML中添加一个背景图像。我添加了属性,但当我运行它时它并没有出现!! 这是图片。

APP

public class App : Application
{
    public App()
    {
        // The root page of your application
        MainPage = new Page();
    }

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="App1.Page"
         BackgroundImage="bg.png">

在此输入图像描述

那么,我该怎么修复它?

在此输入图像描述

6个回答

9
请在每个本地项目中添加您的 bg.png 文件,由于您当前正在使用 Android 模拟器,请从您的 Xamarin.Android 项目开始:

Android - 将图像放置在 Resources/drawable 目录下,Build Action 为:AndroidResource

参考文献:https://developer.xamarin.com/guides/xamarin-forms/working-with/images/

例如,在您的 Xamarin.Android 项目中,按如下所示添加 bg.png

enter image description here

检查该图像的 Build Action 并确保它已分配 AndroidResource。重新构建和重新测试。

你好,SushiHangover, 我刚刚做了那个,但还是没有出现 =x - Biellx
@Biellx 我在我的回答中添加了一些信息,请确保图像位于正确的文件夹中并分配了正确的构建操作。清理、重建并重新部署到您的设备/模拟器... - SushiHangover
是的,我认为问题在于构建操作,我该如何将构建操作设置为该图像? - Biellx
右键单击图像并查看属性。 - SushiHangover

5
在 Xamarin.forms 中,
  1. The images should be placed in the following folders

       iOS, Android  - Resources folder 
    
       Windows/UWP, Windows Phone  - Assets folder 
    
  2. Then the build action(rt click img->properties) of the images should be changed as follows

    iOS - BundleResource             Windows Phone - Content
    
    Android - AndroidResource        Windows/UWP - Content
    
如果图像仍未显示,请尝试在图片属性中将复制到输出目录更改为如果较新则复制

4
如果你想在Xamarin项目的整个页面中添加背景图片,在XAML文件中使用BackgroundImage属性,并将图片添加到Android项目中的Resources -> drawable文件夹和iOS项目的Resources文件夹中。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:PhoneDailerDemo"
             x:Class="PhoneDailerDemo.MainPage"
             BackgroundImage="image3.jpg">

    <Label Text="Welcome to Xamarin Forms!" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />
    <StackLayout Padding="100">
       //..........
    </StackLayout>
</ContentPage>

如果我需要减少图像的不透明度,我能否在应用中动态地进行操作? - Ali123

1

减小图片的尺寸对我有帮助。


1

另一种实现此功能的方法(source)是将图像的构建操作(在文件属性中)设置为嵌入式资源。

然后,使用转换器标记扩展,您将能够直接在XAML中使用它,而无需在每个特定于平台的项目中复制或链接文件。

这是您应该添加到可移植项目中的转换器:

[ContentProperty(nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
  static readonly Assembly CurrentAssembly = 
    typeof(ImageResourceExtension).GetType().Assembly;

  public const string Assets = nameof(Assets);

  public string Source { get; set; }

  public object ProvideValue(IServiceProvider serviceProvider)
  {
    if (string.IsNullOrWhiteSpace(Source))
      return null;

    // Do your translation lookup here, using whatever method you require            
    var source = $"{CurrentAssembly.GetName().Name}.{Assets}.{Source}";

    var imageSource = ImageSource.FromResource(source, CurrentAssembly);

    return imageSource;
  }
}

然后在你的 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"
   xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
   x:Class="WorkingWithImages.EmbeddedImagesXaml">
  <Image Source="{local:ImageResource Background.jpg}"}
</ContentPage>

0

图像文件可以添加到每个应用程序项目中,并从Xamarin.Forms共享代码引用。要在所有应用程序中使用单个图像,必须在每个平台上使用相同的文件名,并且它应该是一个有效的Android资源名称(即只允许使用小写字母,数字,下划线和句点)。

  • iOS - 将图像放置在具有构建操作:BundleResource的Resources文件夹中。图像的Retina版本也应该被提供 - 带有@2x或@3x后缀的文件名前缀分别为两倍和三倍的分辨率(例如myimage@2x.png)。
  • Android - 将图像放置在Resources / drawable目录中,并具有Build Action:AndroidResource。图像的高DPI和低DPI版本也可以被提供(在适当命名的Resources子目录中,例如drawable-ldpi,drawable-hdpi和drawable-xhdpi)。
  • Windows Phone - 将图像放置在应用程序的根目录中,并具有Build Action:Content。
  • Windows / UWP - 将图像放置在应用程序的根目录中,并具有Build Action:Content。

您可以阅读更多信息处理图像在Xamarin.Forms中加载和显示图像


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接