簡體   English   中英

Xamarin形式的EmbeddedImage:如何在XAML中綁定它

[英]EmbeddedImage in Xamarin Forms: how to bind it in XAML

我試圖遵循有關嵌入式圖像的Xamarin文檔: https : //developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images

這是我在便攜式應用程序中使用的代碼片段:

var embeddedImage = new Image { Aspect = Aspect.AspectFit };
embeddedImage.Source = ImageSource.FromResource("<MyNamespace>.<MyFolder>.imageFileName.jpg");

listItem.EmbeddedImage = embeddedImage;

現在,我嘗試將其作為ListView一部分在XAML中進行綁定( 注意: <Image Source="{Binding EmbeddedImage}" />部分可能是錯誤的):

<ListView  x:Name="listView">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout BackgroundColor="#eee"
        Orientation="Vertical">
          <StackLayout Orientation="Horizontal">
            <Image Source="{Binding EmbeddedImage}" /> // This is most likely wrong since it doesn't work       
          </StackLayout>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

我已經使用<Image Source="{Binding ImageUrl}" />並且可以正常工作,因此其余代碼似乎可以正常工作。


編輯:

有人建議將此帖子作為解決方案:

如何在Xamarin.Forms上將圖像加載到ImageCell?

但是那里的公認答案是:

圖片進入您的“本地”項目

...但是實際上Xamarin文檔說它可以在可移植項目中完成(因此您不需要通過所有“本地”項目復制相同的圖像/圖標)。

我知道將它們放在每個子項目中可能會起作用,但這不是我的問題的重點。

您可以使用pack://application uri在xaml中直接訪問資源。

<ListView  x:Name="listView">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout BackgroundColor="#eee"
        Orientation="Vertical">
          <StackLayout Orientation="Horizontal">
            <Image Source="pack://application:,,,/{AssemblyName};component/Images/MyImage.png" />     
          </StackLayout>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

我為此進行了梳理,問題是Microsoft並未在Images文檔中告訴您整個故事。 您必須轉到“文件”文檔以了解為什么它不起作用。

是的,您可以在共享項目目錄中使用嵌入式資源,但是顯然只能在以下兩種情況之一中使用:

  1. 您必須使用標記擴展,然后直接在Xaml中指定資源路徑例如: <Image Source="{local:ImageResource ProjectName.Images.flower.jpg"/>
  2. 您在后面的代碼中編寫了編譯器指令,這些指令自定義每個平台的ResourceID的開頭。

選項1需要對路徑進行硬編碼,因此您不能使用動態分配的圖像,而選項2破壞了嘗試編寫跨平台代碼的整個工作。


巨大的編輯:可以解決

Xamarin論壇上的這篇文章對我產生了巨大的影響: https : //forums.xamarin.com/discussion/17953/helpful-shared-project-image-information

注意:第一篇文章基本上為您提供了所需的所有代碼,但是在線程中稍稍有些不足,可能需要一些有關項目配置的建議才能使其正常工作。

它基本上引入了一些自定義代碼,這些自定義代碼在運行時以編程方式派生資源ID,因此,它是跨平台代碼,可針對當前正在運行的任何平台進行自定義。 為我而努力! 這是我正在使用的建議方法的版本:

using System.Linq;
using System.Reflection;
using System.Diagnostics;
using System;

namespace Helpers
{
    public class EmbeddedSourceror
    {
        public static Xamarin.Forms.ImageSource SourceFor(string pclFilePathInResourceFormat)
        {
            var resources = typeof(EmbeddedSourceror).GetTypeInfo().Assembly.GetManifestResourceNames();
            var resourceName = resources.Single(r => r.EndsWith(pclFilePathInResourceFormat, StringComparison.OrdinalIgnoreCase));
            Debug.WriteLine("EmbeddedSourceror: resourceName string is " + resourceName);

            return Xamarin.Forms.ImageSource.FromResource(resourceName);
        }
    }
}

這使您可以以完全正常的方式編寫Xaml代碼,例如:

<Image
            x:Name         ="backingImage"
            Aspect         ="AspectFill" />

在代碼背后,您要做的就是:

    backingImage.Source = EmbeddedSourceror.SourceFor("flower.jpg");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM