WPF窗口中的图像在通过反射启动时抛出异常 [英] Images in a WPF Window throw exception when launched via reflection

查看:246
本文介绍了WPF窗口中的图像在通过反射启动时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,我将图像作为资源添加到项目中(而不是来自项目设置,如第二个答案如何做到这里),因此它们位于一个文件夹下。我在xaml中使用它们:

I have a WPF application where I added the images as resources to the project (not from project settings, like how the 2nd answer does it here), so they are under a folder. I use them like this in xaml:

<Image Width="32" Height="32" Source="/Images/Effect.png" />

当我从visual studio或explorer手动运行程序时,图像显示正常,没有错误等等。

When I run the program manually, either from visual studio or explorer, the images show up fine, no error, etc.

但是当我通过反射从另一个应用程序运行它时,它会在visual studio中打印这些异常:

But when I run it from another application via reflection, it prints these exceptions inside visual studio:


System.Windows.Data错误:6:
'TargetDefaultValueConverter'
转换器无法转换价值
'/ Images / Effect.png'(类型'串');如果
可用,将使用
后备值。
BindingExpression:Path = EffectIcon;
DataItem ='Node'(HashCode = 34743541);
目标元素是'Image'(Name ='');
目标属性是'Source'(类型
'ImageSource')
IOException:'System.IO.IOException:
找不到资源
'picture / effect.png '。

System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '/Images/Effect.png' (type 'String'); fallback value will be used, if available. BindingExpression:Path=EffectIcon; DataItem='Node' (HashCode=34743541); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') IOException:'System.IO.IOException: Cannot locate resource 'images/effect.png'.

最后,应用程序启动正常但所有图像都丢失了。

In the end, the application starts up fine but all images are missing.

关于如何解决这个问题的任何想法?

Any idea on how to fix this?

我应该以不同的方式绑定图像吗?

Should I be binding the images in a different way?

推荐答案

问题可能在于通过反射实例化模块的程序集中搜索图像。解决此问题的一种可能性是,您指定图像的完整路径,包括程序集名称。类似于:

The problem is probably that the images will be searched in the assembly from where you instantiated your modules via reflection. One possibility to resolve this problem is, that you assign the full path to the images, including the assembly name. Something like:

Source="/AssemblyName;component/image/effect.png"

这篇关于WPF窗口中的图像在通过反射启动时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆