Source = null的ImageSourceConverter错误

Kai*_*i G 47 .net wpf exception-handling image ivalueconverter

我正在将Image的Source属性绑定到字符串.此字符串可能为null,在这种情况下,我只是不想显示图像.但是,我在Debug输出中得到以下内容:

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

我更喜欢这个没有显示,因为它只是噪音 - 是否有任何方法可以抑制它?

Pat*_*Pat 89

@AresAvatar建议您使用ValueConverter是正确的,但该实现对此情况没有帮助.这样做:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

返回DependencyProperty.UnsetValue还解决了抛出(并忽略)所有这些异常的性能问题.返回a new BitmapSource(uri)也可以摆脱异常,但仍有性能损失(并且没有必要).

当然,你还需要管道:

在资源方面:

<local:NullImageConverter x:Key="nullImageConverter"/>
Run Code Online (Sandbox Code Playgroud)

你的形象:

<Image Source="{Binding Path=ImagePath, Converter={StaticResource nullImageConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

  • 令我惊讶的是,当我考虑图像源的空值时,我的代码现在运行得多快.谢谢你,虽然我建议也处理string.IsNullOrWhitespace情况,如果'value'是一个字符串. (3认同)
  • 有没有办法在绑定中指定DependencyProperty.UnsetValue作为TargetNullValue?它可能不需要转换器? (2认同)

Miy*_*shi 7

我使用了 Pat 的 ValueConverter 技术,效果很好。我还尝试了 TargetNullValue 技术,来自这里的 flobodob ,它也很好用。它更容易和更清洁。

<Image Source="{Binding LogoPath, TargetNullValue={x:Null}}" />
Run Code Online (Sandbox Code Playgroud)

TargetNullValue 更简单,不需要转换器。


scr*_*789 6

将您的图像直接绑定到一个对象上,并在必要时返回“UnsetValue”

<Image x:Name="Logo" Source="{Binding ImagePath}"  />
Run Code Online (Sandbox Code Playgroud)

您的 ViewModel 中的属性:

    private string _imagePath = string.Empty;
    public object ImagePath 
    {
        get
        {
            if (string.IsNullOrEmpty(_imagePath))
                return DependencyProperty.UnsetValue;

            return _imagePath;
        }
        set
        {
            if (!(value is string)) 
                return;

            _imagePath = value.ToString();
            OnPropertyChanged("ImagePath");
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 虽然这可能有效,但我认为帕特的答案更好,因为您只需定义一次转换器 - 使用您的解决方案,您必须为每个图像属性重新实现它。 (2认同)