Why does the Image element give me a design-time error with “add as link”ed images?

走远了吗. 提交于 2020-01-06 12:51:14

问题


I need to display an image, which I've done without problems before, but today I decided to be tricky and use "add as link" instead. Well, now I get:

The file Images/hello.png is not part of the project or its 'Build Action' property is not set to 'Resource'.

Wait... its Build Action is set to Resource. I've seen a Silverlight solution that involves the usage of Merged Dictionaries to share files between Silverlight and WPF projects, but it's not clear to me that this would even apply to my WPF + Image issue.

Has anyone solved this problem before? I could make copies of all of the images, but that seems a little silly if I have a shared repository with clip art and the like.


回答1:


Dave,

I've just tried to add image as a link to plain WPF application. Build action is "Resource" (don't confuse with "Embedded Resource"). I've added it to the root, and refer to it as <Image Source="/file_name.jpg"/> - all works fine.

The message you have is it compile or runtime? If it's a runtime, how do you refer to the image? Do you see it in Reflector, when you open your assembly (it should be under Resources folder)?




回答2:


I have images in one assembly which I want to share into another. I've used Add as Link in my second assembly. In the project where the actual image files are located they are in a Resources\Images folder. In the project which links to those files the links are also in a Resources\Images folder. At runtime a XamlParseException claiming "cannot locate resource" is thrown.

My xaml which is referencing the image is in a UserControls folder.

In the project which actually contains the images the path ..\Resources\Images\Blah.png works fine as expected.

Opening the DLLs in Reflector makes it obvious that in the assembly with the linked images holds the images at the root level - the compiler is not respecting the folder location. So in the project with the linked files I have to use ..\Blah.png to find the resource.

Surely a bug in the compiler?




回答3:


Definitely a bug in MSBuild. See Linked Files within a Folder Structure



来源:https://stackoverflow.com/questions/2419530/why-does-the-image-element-give-me-a-design-time-error-with-add-as-linked-imag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!