在 Xamarin Forms 用户 Control/Class 库中引用图像

Referencing Images in Xamarin Forms User Control/Class Library

我已经为我的 Xamarin Forms 项目构建了一个用户控件,从 Xamarin.Forms Class 库开始。用户控件包含一个图像文件,我已将其添加到 "Assets" 目录中的 Class 库项目中。在用户控制代码的 XAML 中,我只是将图像引用为...

<Image Source="Assets/ImageFile.png"/>

我认为这里不需要特定于平台的代码,因为图像文件是 Class 库项目的本地文件并已编译到其中。

当我在 Xamarin Forms 项目中引用 DLL 时,一切都按预期工作...除了没有图像。就好像Class图书馆看不到一样。 我试过路径,但结果总是一样:没有图像。

但是,如果我将图像文件放入 Xamarin Forms 项目(即放入 UWP 中的资产目录),图像看起来很好——即使我仍在引用 Class 库通过 DLL。

我错过了什么?当然,我可以将图像嵌入到 DLL 中,是吗?

要确保图像包含在您的 DLL 中:

您需要设置 构建操作:EmbeddedResource

To embed an image in a project, right-click to add new items and select the image/s you wish to add. By default the image will have Build Action: None; this needs to be set to Build Action: EmbeddedResource.

如果您需要知道在哪里设置:

The Build Action can be viewed and changed in the Properties window for a file.

您可以阅读更多内容here