Source = null的ImageSourceConverter错误 [英] ImageSourceConverter error for Source=null

查看:1066
本文介绍了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)
{
//根据https://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 https://msdn.microsoft.com/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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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