ImageSourceConverter

编程入门 行业动态 更新时间:2024-10-26 00:25:29
本文介绍了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

解决方案>

如果找不到该文件,您会得到该异常。首先你应该在代码中使用 BitmapImage ,转换器用于XAML解析器。其次,相对路径将被解释为指向应用程序中的资源,如果不是这样,并且您想要指向与可执行文件相对的文件,那么应该在 pack:// siteoforigin:,, (

>)。

I'm trying to load an external XML file that contains references to images in WPF.

<page> <foreground path="/data/images/attract/slide1/foreground.png"> <background path="/data/images/attract/slide1/background.png"> </page>

I'm parsing the XML and using the following code to convert each path attribute to an ImageSource.

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;

The backgroundString I'm parsing is not null when I write it to the debug output. However, I'm getting the following error.

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)

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 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).

更多推荐

ImageSourceConverter

本文发布于:2023-11-17 03:22:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ImageSourceConverter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!