2016-08-22 71 views
3

我刚开始了一个Xamarin.Forms应用程序,我想为我的XAML添加背景图片。 我添加了属性,但它在运行时不会显示! 这是图像。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"> 

enter image description here

那么,该如何解决呢?

enter image description here

回答

4

添加您bg.png文件中的每一本机项目,因为您目前正在使用您的Xamarin.Android项目Android模拟器启动:

的Android - 在资源将图像/绘制目录与建设行动:AndroidResource

ref:https://developer.xamarin.com/guides/xamarin-forms/working-with/images/

例子:在你的Xamarin.Android项目,如添加bg.png

enter image description here

检查图像的Build Action,并确保它被分配AndroidResource。重建并重新测试。

+0

你好SushiHangover, 我只是这样做,静静的,没有出现= X – Biellx

+0

@Biellx我加了一些信息,以我的回答,请确保该图像是在正确的文件夹,并指派了正确生成操作。清理并重建并重新部署到您的设备/仿真程序... – SushiHangover

+0

Yeaa,我认为问题是构建操作,我如何将构建操作设置为该图像?] – Biellx

0

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

  • iOS - 将图像放置在具有Build Action:BundleResource的Resources文件夹中。还应提供图像的视网膜版本 - 文件扩展名前的文件名后缀为分辨率的两倍和三倍,后缀为@ 2x或@ 3x(例如,[email protected])。
  • Android - 使用Build Action:AndroidResource将图像放置在Resources/drawable目录中。还可以提供图像的高DPI版本和低DPI版本(在适当命名的Resources子目录中,例如drawable-ldpi,drawable-hdpi和drawable-xhdpi)。
  • Windows Phone - 将图像放置在应用程序的根目录中,其中包含Build Action:Content。
  • Windows/UWP - 将图像放置在应用程序的根目录中并使用生成操作:内容。

你可以阅读更多的Working with Images Loading and displaying images in Xamarin.Forms

3

在Xamarin。形式

  1. 的图像应该放置在以下文件夹

    iOS, Android - Resources folder 
    
        Windows/UWP, Windows Phone - Assets folder 
    
  2. 然后生成操作(RT点击img->性)的图像的应改变如下

    iOS - BundleResource    Windows Phone - Content 
    
    Android - AndroidResource  Windows/UWP - Content 
    

如果仍未显示图像,请尝试更改复制到输出Directo ry复制,如果更新图像属性

0

缩小图像的大小为我工作。

1

如果您想在Xamarin项目的整个页面的XAML文件中添加背景图像,那么使用BackgroundImage属性并将您的图像添加到资源 - >可绘制文件夹和iOS资源文件夹下的Android项目。

<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>