在共享项目中处理嵌入图像
Working with embedded images in a shared project
我想将嵌入的图像添加到我的共享项目中(否 PCL)。我将图像作为 嵌入式资源 添加到共享项目(非特定平台)。如果我使用以下内容,我就可以使用嵌入的图像:
ImageSource.FromResource("TestProject.Droid.Images.Document Folder.png")
只需使用字符串
ProjectNamespace.Subfolder.Filename.ext
并不像 docs 所描述的那样工作。必须添加平台名称空间(iOS、Droid、...)。
另一个可行的选项是编辑 .projitems。我添加了 LogicalName:
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Images\Document Folder.png">
<LogicalName>Document Folder.png</LogicalName>
</EmbeddedResource>
</ItemGroup>
但这不是开箱即用的东西。没有 Resource ID. I also tried looking into the sample project,但根本无法启动。
如何在共享项目中使用嵌入图像?
official documentation 只处理PCL 项目。对于共享项目,您要么必须使用完整字符串,要么添加 LogicalName
.
完整字符串:
从完整字符串加载资源
ImageSource.FromResource("TestProject.Droid.Images.Document Folder.png")
字符串是这样构建的
<ProjectNamespace>.<PlatformNameSpace>.{<Subfolder>}.<Filename>.<Fileextension>
<Subfolder>
仅当您在其中有图像并且必须为每个子文件夹添加字符串时才使用。所以你只需要一种机制来获取每个平台的完整命名空间。也许你定义了一个 属性 其中 returns 一个不同的字符串(由你自己管理)取决于平台。
逻辑名称:
编辑 .projitems 并为每个图像添加 LogicalName
:
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Images\Document Folder.png">
<LogicalName>Document Folder.png</LogicalName>
</EmbeddedResource>
</ItemGroup>
那你可以这样做
ImageSource.FromResource("Document Folder.png")
前面提到可以在Visual Studio中直接添加资源ID,就不用自己添加LogicalName
了,但这似乎不是真的。
所以没有更简单的方法。 PCL 和共享项目之间的区别也有解释 here(法语)。
Working with files in Xamarin.Forms 文章还解释了一些重要的细节。
我想将嵌入的图像添加到我的共享项目中(否 PCL)。我将图像作为 嵌入式资源 添加到共享项目(非特定平台)。如果我使用以下内容,我就可以使用嵌入的图像:
ImageSource.FromResource("TestProject.Droid.Images.Document Folder.png")
只需使用字符串
ProjectNamespace.Subfolder.Filename.ext
并不像 docs 所描述的那样工作。必须添加平台名称空间(iOS、Droid、...)。
另一个可行的选项是编辑 .projitems。我添加了 LogicalName:
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Images\Document Folder.png">
<LogicalName>Document Folder.png</LogicalName>
</EmbeddedResource>
</ItemGroup>
但这不是开箱即用的东西。没有 Resource ID. I also tried looking into the sample project,但根本无法启动。
如何在共享项目中使用嵌入图像?
official documentation 只处理PCL 项目。对于共享项目,您要么必须使用完整字符串,要么添加 LogicalName
.
完整字符串:
从完整字符串加载资源
ImageSource.FromResource("TestProject.Droid.Images.Document Folder.png")
字符串是这样构建的
<ProjectNamespace>.<PlatformNameSpace>.{<Subfolder>}.<Filename>.<Fileextension>
<Subfolder>
仅当您在其中有图像并且必须为每个子文件夹添加字符串时才使用。所以你只需要一种机制来获取每个平台的完整命名空间。也许你定义了一个 属性 其中 returns 一个不同的字符串(由你自己管理)取决于平台。
逻辑名称:
编辑 .projitems 并为每个图像添加 LogicalName
:
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Images\Document Folder.png">
<LogicalName>Document Folder.png</LogicalName>
</EmbeddedResource>
</ItemGroup>
那你可以这样做
ImageSource.FromResource("Document Folder.png")
前面提到可以在Visual Studio中直接添加资源ID,就不用自己添加LogicalName
了,但这似乎不是真的。
所以没有更简单的方法。 PCL 和共享项目之间的区别也有解释 here(法语)。
Working with files in Xamarin.Forms 文章还解释了一些重要的细节。