解决方案绑定设置Image.Source为绑定为空引发的错误。 (SUP pression?) [英] Solution to binding errors thrown by setting Image.Source to a binding to null. (suppression?)
问题描述
我有一种情况,我必须设置为null图像源将在稍后解决,然后显示出来。以$ P $什么好的办法从抛出绑定错误pvent我的code?
一个例子:
System.Windows.Data错误:23:不能'从类型''转换为键入'System.Windows.Media.ImageSource'为'EN-US使用默认的转换文化;考虑使用绑定的转换器属性。 NotSupportedException异常:的System.NotSupportedException:ImageSourceConverter无法从(空)转换。
在System.ComponentModel.TypeConverter.GetConvertFromException(对象的值)
在System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext方面,CultureInfo的文化,对象的值)
在MS.Internal.Data.DefaultValueConverter.ConvertHelper(对象o,类型destinationType,DependencyObject的targetElement,CultureInfo的文化,布尔isForward)
块引用>编辑:按要求
<图像X:NAME =形象HEIGHT =自动WIDTH =自动不透明度=0>
< Image.Effect>
< DropShadowEffect />
< /Image.Effect>
< Image.Source>
<绑定路径=的ImageStream>
< Binding.ValidationRules>
< validationRules:NotNullStreamValidationRule />
< /Binding.ValidationRules>
< /装订>
< /Image.Source>
< /图像>
命名空间FlickrDemo.ViewModel
{公共类FlickrPhotoViewModel:ViewModelBase
{
公共常量字符串ImageStreamPropertyName =的ImageStream; 专用流_imageStream = NULL; 公共流的ImageStream
{
得到
{
返回_imageStream;
} 组
{
如果(_imageStream ==值)
{
返回;
}
_imageStream =价值; RaisePropertyChanged(ImageStreamPropertyName);
}
} 公共常量字符串IsLoadingPropertyName =IsLoading; 私人布尔_isLoading = FALSE; 公共BOOL IsLoading
{
得到
{
返回_isLoading;
} 组
{
如果(_isLoading ==值)
{
返回;
} _isLoading =价值; RaisePropertyChanged(IsLoadingPropertyName);
}
} 公共常量字符串PhotoIDPropertyName =PHOTOID; 私人字符串_photoID =的String.Empty; 公共字符串PHOTOID
{
得到
{
返回_photoID;
} 组
{
如果(_photoID ==值)
{
返回;
} VAR属性oldValue = _photoID;
_photoID =价值; RaisePropertyChanged(PhotoIDPropertyName);
}
} 公共FlickrPhotoViewModel(字符串PHOTOID)
{
this.PropertyChanged + =异步(S,E)=>
{
如果(e.PropertyName == ImageStreamPropertyName)
{
如果(!(的ImageStream == NULL ||的ImageStream == Stream.Null))
{
IsLoading = FALSE;
}
}
};
IsLoading = TRUE;
PHOTOID = PHOTOID;
}
}
}
解决方案问题是你的
的ImageStream
属性的数据类型。有没有转换器,它知道如何处理空值的情况:的 System.Windows.Data错误:23:不能'从类型''转换为键入'System.Windows.Media.ImageSource'为'EN-US使用默认的转换文化; 考虑使用绑定的转换器属性的
解决这个问题的一种可能性是使自己的的IValueConverter 实施。如果输入的值是一个流返回。如果没有,返回null。如果这也不行,返回一个空的虚
的ImageSource
。是这样的:
公共类ImageStreamForwardConverter:{的IValueConverter
公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化){
如果(空==值){
返回null;
}否则如果(价值流){
返回值;
}其他{
抛出新的InvalidOperationException异常(不支持的类型);
}
....I've got a scenario where I have Image Sources set to null which are resolved later and then displayed. Any good way to prevent my code from throwing binding errors?
An example:
System.Windows.Data Error: 23 : Cannot convert '' from type '' to type 'System.Windows.Media.ImageSource' for 'en-US' 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)'
Edit: By request
<Image x:Name="image" Height="Auto" Width="Auto" Opacity="0"> <Image.Effect> <DropShadowEffect/> </Image.Effect> <Image.Source> <Binding Path="ImageStream"> <Binding.ValidationRules> <validationRules:NotNullStreamValidationRule/> </Binding.ValidationRules> </Binding> </Image.Source> </Image> namespace FlickrDemo.ViewModel { public class FlickrPhotoViewModel : ViewModelBase { public const string ImageStreamPropertyName = "ImageStream"; private Stream _imageStream = null; public Stream ImageStream { get { return _imageStream; } set { if (_imageStream == value) { return; } _imageStream = value; RaisePropertyChanged(ImageStreamPropertyName); } } public const string IsLoadingPropertyName = "IsLoading"; private bool _isLoading = false; public bool IsLoading { get { return _isLoading; } set { if (_isLoading == value) { return; } _isLoading = value; RaisePropertyChanged(IsLoadingPropertyName); } } public const string PhotoIDPropertyName = "PhotoID"; private string _photoID = String.Empty; public string PhotoID { get { return _photoID; } set { if (_photoID == value) { return; } var oldValue = _photoID; _photoID = value; RaisePropertyChanged(PhotoIDPropertyName); } } public FlickrPhotoViewModel(string photoID) { this.PropertyChanged += async (s, e) => { if (e.PropertyName == ImageStreamPropertyName) { if (!(ImageStream == null || ImageStream == Stream.Null)) { IsLoading = false; } } }; IsLoading = true; PhotoID = photoID; } } }
解决方案The problem is the datatype of your
ImageStream
property. There is no converter that knows how to handle the null-situation:System.Windows.Data Error: 23 : Cannot convert '' from type '' to type 'System.Windows.Media.ImageSource' for 'en-US' culture with default conversions; consider using Converter property of Binding
One possibility to solve this problem is to make your own IValueConverter implementation. If the input value is a stream, return it. If not, return null. If this does not work, return an empty dummy
ImageSource
.Something like:
public class ImageStreamForwardConverter : IValueConverter{ public object Convert(object value, Type targetType, object parameter, CultureInfo culture){ if(null == value){ return null; }else if(value is Stream){ return value; }else{ throw new InvalidOperationException("Unsupported type"); } ....
这篇关于解决方案绑定设置Image.Source为绑定为空引发的错误。 (SUP pression?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!