打包 URI 和路径无法解析 WPF 中的图像
我有以下目录结构
Project
\Images
+view.png
control.xaml
,并且在控件中我有一个由以下 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”都不起作用。据我了解,这是同一个问题此问题 提出。但是,我遇到了同样的错误。令人困惑的是,在 Visual Studio 2008 的设计器中,图像渲染正确,但在调用 InitializeComponent() 时,我得到:
无法将属性“Source”中的字符串“pack://application:,,,/images/view.png”转换为“System.Windows.Media.ImageSource”类型的对象。无法找到资源“images/view.png”。标记文件“RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml”第 61 行第 40 行中的对象“System.Windows.Controls.ControlTemplate”出错。
我认为也许有一个我必须声明的命名空间,但根据 msdn site 我相信我不需要做任何事情像那样。
I have the following directory structure
Project
\Images
+view.png
control.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>
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我实际上让它工作,但必须将我的源设置为
/ProjectName;component/images/view.png
因为我将 ProjectName 作为引用的程序集,所以它与 Path: 部分相同在我在问题中引用的msdn页面。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.将“view.png”的构建操作设置为资源而不是内容,这个问题就会消失。我能够通过这种方式重现您的问题,并且当设置为资源时它可以正常工作。
Set the Build Action for 'view.png' to Resource instead of Content and this problem should go away. I was able to reproduce your problem this way and it works correctly when set as a Resource.
从应用程序文件夹和设计页面中调用图像
Xaml.VB 中的页面加载
Call LoadImages()
Xaml 设计页面
Call the Image from Application folder and Design Page
Page load in Xaml.VB
Call LoadImages()
Xaml Design Page