打包URI和路径无法解析WPF中的图像 [英] Pack URI and path not resolving image in WPF
问题描述
我具有以下目录结构
Project
\Images
+view.png
control.xaml
在控件中,我有一个由以下XAML定义的按钮:
and in the control I have a button defined by the following XAML:
<Button Click="Search"
Grid.Column="1"
Margin="0,5,5, 0"
HorizontalAlignment="Right">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Image Source="pack://application:,,,/images/view.png"
Width="16"
Height="16"
ToolTip="Search"
Cursor="Hand"
Opacity="0.8" />
</ControlTemplate>
</Button.Template>
</Button>
但是,这个包URI方法和"/images/view.png"都不起作用.据我了解,这是相同的问题
However, neither this pack URI method nor the "/images/view.png" is working. As I understand it, this is the same issue this question raises. However, I get the same error. The confusing thing is that in designer in Visual Studio 2008, the image renders correctly, but on the call to the InitializeComponent() call, I get:
无法将属性源"中的字符串"pack://application:、、//images/view.png"转换为类型"System.Windows.Media.ImageSource"的对象.无法找到资源"images/view.png".标记文件'RecapSpecEditControl; component/modaltreadgroupdatadialog.xaml'第61行位置40处的对象'System.Windows.Controls.ControlTemplate'错误.
Cannot convert string 'pack://application:,,,/images/view.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'images/view.png'. Error at object 'System.Windows.Controls.ControlTemplate' in markup file 'RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml' Line 61 Position 40.
I thought that maybe there was a namespace that I had to declare but according to the msdn site I believe I don't have to do anything like that.
推荐答案
我实际上可以使用它,但是必须将源设置为"/ProjectName;component/images/view.png",因为我将ProjectName作为引用的程序集,这与我在问题中引用的msdn页面上的Path:部分相同.
I actually got this to work, but had to set my source to "/ProjectName;component/images/view.png" Because I have the ProjectName as a referenced assembly this is then the same as the Path: portion at the msdn page that I referenced in the question.
这篇关于打包URI和路径无法解析WPF中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!