我有一个 WPF 应用程序,我在其中将图像作为资源添加到项目中(不是来自项目设置,就像第二个答案是如何做到的 here ),所以它们在一个文件夹下。我在 xaml 中这样使用它们:
<Image Width="32" Height="32" Source="/Images/Effect.png" />
当我从 visual studio 或 explorer 手动运行程序时,图像显示正常,没有错误等。
但是当我通过反射从另一个应用程序运行它时,它会在 visual studio 中打印这些异常:
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'.
最后,应用程序启动正常,但所有图像都丢失了。
知道如何解决这个问题吗?
我应该以不同的方式绑定(bind)图像吗?
最佳答案
问题可能是图像将从您通过反射实例化模块的程序集中搜索。解决此问题的一种可能性是,您为图像分配完整路径,包括程序集名称。像这样的东西:
Source="/AssemblyName;component/image/effect.png"
关于c# - WPF 窗口中的图像在通过反射启动时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807310/