Xamarin Forms将资源图像作为XAML中ContentPage的背景嵌入

时间:2016-12-17 15:41:35

标签: c# xaml xamarin xamarin.forms

我是第一次尝试Xamarin表格,所以这可能是一个写得很糟糕的问题,请原谅我。

我有一个图像作为Embeded资源: screenshot 对于ContentPage,我可以在XAML中将该图像用作BackgroundImage吗?如何

提前谢谢大家。

更新,如此处所建议: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images

  1. 我将自定义xaml标记扩展添加到主页cs:
  2. [Xamarin.Forms.ContentProperty("Source")]
    public class ImageResourceExtension : Xamarin.Forms.Xaml.IMarkupExtension
    {
        public string Source { get; set; }
    
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }
            // Do your translation lookup here, using whatever method you require
            var imageSource = Xamarin.Forms.ImageSource.FromResource(Source);
    
            return imageSource;
        }
    }
    
    1. 我现在可以使用这样的图像:
    2.   <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
          <!-- use a custom Markup Extension -->
          <Image Source="{local:ImageResource App2.images.back_01.png}" />
        </StackLayout>
      
      1. 仍然是我无法回答的原始问题,如何将其作为背景
      2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:local="clr-namespace:App2;assembly=App2"
                     x:Class="App2.MainPage"
                     BackgroundImage="">
        

2 个答案:

答案 0 :(得分:0)

似乎只能将图像嵌入式资源设置为便携式,适用于除 BackgroundImage之外的所有图像用途
要用于BackgroundImage,图像必须嵌入在每个平台资源中(必须选择正确的每个平台构建操作)。
然后,对于BackgroundImage,它可以像这样被命名:“folder_name / file_name.png” 因此,必须多次执行一次(而不是其他图像使用)。

以上对我来说是一个解决方案。
如果有人知道如何在没有多余操作的情况下执行此操作,请发布。

答案 1 :(得分:-2)

将图像放入文件夹:

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:App2;assembly=App2"
             x:Class="App2.MainPage"
             BackgroundImage="back01.png">