2016-12-17 163 views
0

我想第一次使用Xamarin表单,所以这可能是一个写得不好的问题,请原谅我,如果是的话。Xamarin形式为XAML中ContentPage的背景嵌入资源图像

我有一张照片作为嵌入式资源: screenshot 我是否可以使用图像的BackgroundImage在XAML,为ContentPage?如何

谢谢大家提前。

UPDATE,作为这里提出: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images

  1. 我添加的自定义XAML标记扩展到主页CS:
[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; 
    } 
} 
  • 我现在可以使用像这样的图像:
  • <StackLayout VerticalOptions="Center" HorizontalOptions="Center"> 
        <!-- use a custom Markup Extension --> 
        <Image Source="{local:ImageResource App2.images.back_01.png}" /> 
        </StackLayout> 
    
  • 还是原来的问题我不能回答,如何使它背景
  • <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=""> 
    

    回答

    0

    它似乎在制作图像嵌入式资源只有在便携式,适用于所有图像用途e xcept BackgroundImage。
    要用于BackgroundImage,图像必须是也嵌入在每平台资源资源(必须选择正确的每个平台生成操作)。
    然后,对于BackgroundImage,可以像这样来引用:“folder_name/file_name.png” 因此,它必须做几次而不是一次(而不是其他图像用法)。

    以上对我来说是一种解决方案。
    如果有人知道如何做到这一点没有多余的行为,请张贴。

    -2

    认沽图像文件夹:

    AndroidResources/drawable

    iOSResources

    然后尝试

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

    感谢您的答复,但它几乎一样解决方案整个观点是仅将图像作为便携式部件中的嵌入式资源,而不是每个平台,这对背景图像来说似乎是不可能的。 –