简体   繁体   English

在Xamarin表单用户控件/类库中引用图像

[英]Referencing Images in Xamarin Forms User Control/Class Library

I've built a user control for my Xamarin Forms projects, starting with a Xamarin.Forms Class Library. 我已经从Xamarin.Forms类库开始为Xamarin Forms项目构建了一个用户控件。 The user control contains an image file that I've added to the Class Library project in an "Assets" directory. 用户控件包含一个图像文件,该图像文件已添加到“资产”目录中的“类库”项目中。 Within the user control code's XAML I'm simply referencing the image as... 在用户控制代码的XAML中,我只是将图像引用为...

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

I'm thinking there's no need for platform-specific code here since the image file is local to the Class Library project and compiled into it. 我认为这里不需要平台特定的代码,因为图像文件是类库项目的本地文件,并已编译到该文件中。

When I reference the DLL in a Xamarin Forms project, everything works as expected...EXCEPT, there's no image. 当我在Xamarin Forms项目中引用DLL时,一切都按预期工作...除了,没有图像。 It's as if the Class Library can't see it. 好像类库看不到它。 I've played around with the path, but the result is always the same: no image. 我已经尝试过了,但是结果总是一样的:没有图像。

However, if I drop the image file into the Xamarin Forms project (ie, into the Assets directory in UWP), the image appears just fine -- even though I'm still referencing the Class Library through the DLL. 但是,如果我将图像文件放到Xamarin Forms项目中(即,放到UWP中的Assets目录中),则图像看起来就很好-即使我仍通过DLL引用类库。

What am I missing? 我想念什么? Surely I can embed the image within the DLL, yes? 当然可以将图像嵌入DLL中,对吗?

To make sure the image is included within your DLL: 要确保映像包含在您的DLL中:

You need to set the Build Action: EmbeddedResource . 您需要设置Build Action: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. 这需要设置为Build Action:EmbeddedResource。

If you need to know where to set this: 如果您需要知道在哪里设置:

The Build Action can be viewed and changed in the Properties window for a file. 可以在文件的“属性”窗口中查看和更改“构建操作”。

You can read more here . 您可以在这里阅读更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM