ImageSourceConverter - System.NullReferenceException [英] ImageSourceConverter - System.NullReferenceException
问题描述
我正在尝试加载包含对WPF中图片的引用的外部XML文件。
< page&
< foreground path =/ data / images / attract / slide1 / foreground.png>
< background path =/ data / images / attract / slide1 / background.png>
< / page>
我解析XML并使用以下代码将每个路径属性转换为ImageSource。 / p>
string backgroundString =(string)backgroundNode.Attributes [path]。
Debug.WriteLine(backgroundString); // return/data/images/attract/slide1/background.png
avm.BackgroundImage = new ImageSourceConverter()。ConvertFromString(backgroundString)as ImageSource;
当我将其写入调试输出时,我解析的backgroundString不为null。但是,我收到以下错误。
PresentationCore.dll中发生类型为System.NullReferenceException的第一次机会异常
System.NullReferenceException:对象引用未设置为对象的实例。
at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,Object value)
在System.ComponentModel.TypeConverter.ConvertFromString(String text)
有没有人看到我做错了?
感谢您,
Bryan
如果找不到该文件,您会得到该异常。首先你应该在代码中使用 >)。 I'm trying to load an external XML file that contains references to images in WPF. I'm parsing the XML and using the following code to convert each path attribute to an ImageSource. The backgroundString I'm parsing is not null when I write it to the debug output. However, I'm getting the following error. Does anyone see what I'm doing wrong? Any help would be much appreciated. Thanks,
Bryan You get that exception if the file is not being found. Firstly you should use a 这篇关于ImageSourceConverter - System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! BitmapImage
,转换器用于XAML解析器。其次,相对路径将被解释为指向应用程序中的资源,如果不是这样,并且您想要指向与可执行文件相对的文件,那么应该在 pack:// siteoforigin:,, (
<page>
<foreground path="/data/images/attract/slide1/foreground.png">
<background path="/data/images/attract/slide1/background.png">
</page>
string backgroundString = (string)backgroundNode.Attributes["path"].Value;
Debug.WriteLine(backgroundString); //returns "/data/images/attract/slide1/background.png"
avm.BackgroundImage = new ImageSourceConverter().ConvertFromString(backgroundString) as ImageSource;
A first chance exception of type 'System.NullReferenceException' occurred in PresentationCore.dll
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFromString(String text)
BitmapImage
in code, the converters are for the XAML-parser. Secondly relative paths will be interpreted as pointing to a resource in the application, if this is not the case and you want to point to files relative to the executable instead you should prepend pack://siteoforigin:,,,
(see Pack URIs in WPF).