【问题标题】:Xamarin.Forms Xaml background ImageXamarin.Forms Xaml 背景图片
【发布时间】:2016-12-29 15:10:03
【问题描述】:

我刚刚启动了一个 Xamarin.Forms 应用程序,我想将背景图像添加到我的 XAML。 我添加了属性,但是运行时它没有出现!! 这是图片。

应用程序

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

那么,我该如何解决呢?

【问题讨论】:

    标签: xaml xamarin xamarin.android xamarin.forms xamarin-studio


    【解决方案1】:

    在您的每个原生项目中添加您的 bg.png 文件,因为您当前使用的是 Android 模拟器,从您的 Xamarin.Android 项目开始:

    Android - 使用 Build Action: AndroidResource 将图像放置在 Resources/drawable 目录中

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

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

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

    【讨论】:

    • 你好 SushiHangover,我只是这样做了,但仍然没有出现 =x
    • @Biellx 我在答案中添加了一些信息,确保图像位于正确的文件夹中并分配了正确的构建操作。清理并重建并重新部署到您的设备/模拟器......
    • 是的,我认为问题在于构建操作,如何将构建操作设置为该图像?]
    • 右击图片查看属性
    【解决方案2】:

    在 Xamarin.forms 中

    1. 图片应放在以下文件夹中

         iOS, Android  - Resources folder 
      
         Windows/UWP, Windows Phone  - Assets folder 
      
    2. 那么图片的build action(rt click img->properties)应该修改如下

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

    如果仍然没有显示图像,请尝试在图像属性中将复制到输出目录更改为如果更新则复制

    【讨论】:

      【解决方案3】:

      如果要在 XAML 文件中为 Xamarin 项目中的整个页面添加背景图像,请使用 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>
      

      【讨论】:

        【解决方案4】:

        减小图像的大小对我有用。

        【讨论】:

          【解决方案5】:

          实现此目的的另一种方法 (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>
          

          【讨论】:

            【解决方案6】:

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

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

            您可以在Working with Images Loading and displaying images in Xamarin.Forms阅读更多内容

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-05-21
              • 1970-01-01
              • 2013-08-16
              • 1970-01-01
              • 2017-10-05
              • 1970-01-01
              • 2019-03-24
              • 1970-01-01
              相关资源
              最近更新 更多