2016-12-17 83 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

    感謝您的答覆,但它幾乎一樣解決方案整個觀點是僅將圖像作爲便攜式部件中的嵌入式資源,而不是每個平臺,這對背景圖像來說似乎是不可能的。 –