我正在尝试使用本地资源在Button中放置一个图标。 (C#Visual Studio 2012)
直接在我的项目中,我有一个名为“Resources”的文件夹,其中包含“Icons / MyIcon.png”
以下代码有效(但未使用相对路径)
<Button Width="20" Height="20">
<Button.Background>
<ImageBrush ImageSource="C:\Documents\MyProject\Resources\Icons\MyIcon.png">
</ImageBrush>
</Button.Background>
但是,我希望它使用相对路径...所以我尝试了以下
<ImageBrush ImageSource="..\MyProject\Resources\Icons\MyIcon.png">
它编译,但然后给我错误:
{"Cannot locate resource 'myproject/resources/icons/myicon.png'."}
然后我发现了一篇文章,谈到引用该项目并说要做:
"/[ assembly name ];component[ path within the project ]"
所以我试过了:
<ImageBrush ImageSource="/MyProject;component\Resources\Icons\MyIcon.png">
但它不起作用。
它出现在GUI-Builder中......但是当我真正尝试“调试”代码时......它无法运行。
如果我通过添加'123'来改变路径,例如:
<ImageBrush ImageSource="/MyProject;component\Resources\Icons\MyIcon123.png">
我收到预编译错误:“找不到指定的文件”。 一旦我删除'123',错误就会消失。并再次在图表中显示图标。 但是,当我跑...我仍然得到:
{"Cannot locate resource 'resources/icons/myicon.png'."}
发生了什么事?为什么它只能在运行之前找到资源?而不是我真正开始的时候?
答案 0 :(得分:8)
您是否需要将图像的BuildAction设置为Resource?它被装入集会吗?或者你像内容一样对待它?在这种情况下,您需要将BuildAction设置为Content并适当地设置CopyToOutputDirectory。
Here's指向处理资源,内容文件等的MSDN文章的链接
答案 1 :(得分:6)
我知道这是一个老帖子但是对于VS2013这些例子都没有对我有用,但所有这些例子的组合是我现在必须如何引用我的图像资源&amp; URI中的来源..
"pack://application:,,,/!APP NAME!;component/Resources/Purple.bmp"
当然可以为您!APP NAME!
替换"pack://application:,,,/test1;component/Resources/Purple.bmp"
。storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
答案 2 :(得分:2)
我在博文here中介绍了这一点。
对于名为“Images”的文件夹,它基本上看起来像这样。如果.xaml文件位于其他文件夹中,则需要执行../Images/favicon.ico
。请注意,在任何情况下都不需要将项目名称添加到路径的开头。如果您需要更多详细信息,请参阅帖子末尾的示例项目。
<Image Margin="5" Source="/Images/favicon.ico" Height="100"/>
答案 3 :(得分:1)
这适用于我的资源文件夹在Application中的图像的相对路径。
<Image Source="..\Images\down_arrow.gif" />