【发布时间】:2015-01-01 02:24:32
【问题描述】:
我被难住了。我正在尝试显示嵌入在另一个程序集中的图像。我已经尝试了几件事,但到目前为止没有任何效果。我确信我错过了一些非常简单的东西。
目前,Xaml 看起来像这样:
<Image>
<Image.Source>
<BitmapImage UriSource="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
</Image.Source>
</Image>
我最初的 Xaml 看起来像这样:
<Image Source="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
我换了它,希望它会有所作为。 (它没有。)
我在尝试创建图像的项目中引用了Name.Of.Assembly。在程序集Name.Of.Assembly 中,有一个名为 Resources 的目录,其中有一个名为 Icons 的子文件夹,其中包含图像 icon.png。 icon.png 上的构建操作设置为 Embedded Resource。
当我尝试运行时,我得到一个XamlParseException:
'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception'
谁的内在例外是:
'Cannot locate resource 'resources/icons/icon.png'
这就像它没有在程序集中寻找资源。为什么加载图片失败?我错过了什么?
【问题讨论】:
-
尝试用
component/Resources替换component;Resources -
哇!这实际上是我发布我的问题的一个错字。
-
并且构建操作应该设置为
Resource -
@dkozl 我尝试将它切换到资源,但我仍然得到同样的错误:(
标签: wpf image uri bitmapimage pack-uri