Source = null的ImageSourceConverter错误

编程入门 行业动态 更新时间:2024-10-25 15:28:12
本文介绍了Source = null的ImageSourceConverter错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我将一个图像的Source属性绑定到一个字符串。此字符串可能为null,在这种情况下,我不想显示图像。但是,我在调试输出中得到以下内容:

System.Windows.Data错误:23:不能转换'< null>'从类型'< null>'到类型'System.Windows.Media.ImageSource'为'en-AU'文化与默认转换;考虑使用Binding的Converter 属性。 NotSupportedException:'System.NotSupportedException: ImageSourceConverter无法从(null)转换。在 System.ComponentModel.TypeConverter.GetConvertFromException(Object value)at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,Object 值) MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o,Type destinationType, DependencyObject targetElement, CultureInfo culture,Boolean isForward)'

我宁愿不显示,因为它只是噪音 - 有什么办法压制吗?

解决方案

@AresAvatar正确建议您使用ValueConverter,但该实现并不能帮助您。这样做:

public class NullImageConverter:IValueConverter { public object Convert(object value,Type targetType ,object参数,CultureInfo文化) { if(value == null)返回DependencyProperty.UnsetValue; 返回值; } public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture) { //根据msdn.microsoft。 com / en-us / library / system.windows.data.ivalueconverter.convertback(v = vs.110).aspx#Anchor_1 //(kudos Scott Chamberlain),如果你不支持转换 //返回你应该返回一个Binding.DoNothing或 // DependencyProperty.UnsetValue return Binding.DoNothing; //原始代码: // throw new NotImplementedException(); } }

返回 DependencyProperty.UnsetValue 还解决了抛出(并忽略)所有这些异常的性能问题。返回一个新的BitmapSource(uri)也可以摆脱异常,但仍然有一个性能命中(没有必要)。

当然,您还需要管道:

在资源中:

< local:NullImageConverter x:Key =nullImageConverter/>

您的图片:

< Image Source ={Binding Path = ImagePath,Converter = {StaticResource nullImageConverter}}/>

I'm binding the Source property of an Image to a string. This string may be null in which case I just don't want to display an Image. However, I'm getting the following in my Debug output:

System.Windows.Data Error: 23 : Cannot convert '<null>' from type '<null>' to type 'System.Windows.Media.ImageSource' for 'en-AU' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: ImageSourceConverter cannot convert from (null). at System.ComponentModel.TypeConverter.GetConvertFromException(Object value) at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'

I'd prefer if this wasn't displayed as it's just noise - is there any way to suppress it?

解决方案

@AresAvatar is right in suggesting you use a ValueConverter, but that implementation does not help the situation. This does:

public class NullImageConverter :IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return DependencyProperty.UnsetValue; return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { // According to msdn.microsoft/en-us/library/system.windows.data.ivalueconverter.convertback(v=vs.110).aspx#Anchor_1 // (kudos Scott Chamberlain), if you do not support a conversion // back you should return a Binding.DoNothing or a // DependencyProperty.UnsetValue return Binding.DoNothing; // Original code: // throw new NotImplementedException(); } }

Returning DependencyProperty.UnsetValue also addresses the performance issues from throwing (and ignoring) all those exceptions. Returning a new BitmapSource(uri) would also get rid of the exceptions, but there is still a performance hit (and it isn't necessary).

Of course, you'll also need the plumbing:

In resources:

<local:NullImageConverter x:Key="nullImageConverter"/>

Your image:

<Image Source="{Binding Path=ImagePath, Converter={StaticResource nullImageConverter}}"/>

更多推荐

Source = null的ImageSourceConverter错误

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

发布评论

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

>www.elefans.com

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