IValueConverter 应该冻结跨线程安全绑定的图像不起作用
IValueConverter That is supposed to freeze image for cross thread safe binding not working
我的图像绑定不断抛出此错误:
Must create DependencySource on same Thread as the DependencyObject.
我写了一个 IValueConverter
我认为可以解决但似乎不是。
public class FrozenImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ImageSource image = value as ImageSource;
if (image != null)
{
if (image.Dispatcher != null)
{
if (image.Dispatcher.CheckAccess())
{
ImageSource returnImage = new BitmapImage();
returnImage = image;
returnImage.Freeze();
return returnImage;
} else return image.Dispatcher.Invoke(() => Convert(value, targetType, parameter, culture));
} else return image;
} else return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
我是不是漏掉了什么,或者这是不可能的?
还尝试了一个简化的版本,因为调度程序 应该 不需要,这也有同样的错误:
public class FrozenImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ImageSource image = value as ImageSource;
if (image != null)
{
ImageSource returnImage = image.Clone();
returnImage.Freeze();
return returnImage;
} else return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
显然这是不可能的,因为它看起来像是在 IValueConverter
有机会完成它的工作之前抛出异常......如果有人知道解决这个问题的替代方法不涉及迭代来源收集和设置所有图像冻结请让我知道。
我的图像绑定不断抛出此错误:
Must create DependencySource on same Thread as the DependencyObject.
我写了一个 IValueConverter
我认为可以解决但似乎不是。
public class FrozenImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ImageSource image = value as ImageSource;
if (image != null)
{
if (image.Dispatcher != null)
{
if (image.Dispatcher.CheckAccess())
{
ImageSource returnImage = new BitmapImage();
returnImage = image;
returnImage.Freeze();
return returnImage;
} else return image.Dispatcher.Invoke(() => Convert(value, targetType, parameter, culture));
} else return image;
} else return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
我是不是漏掉了什么,或者这是不可能的?
还尝试了一个简化的版本,因为调度程序 应该 不需要,这也有同样的错误:
public class FrozenImage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
ImageSource image = value as ImageSource;
if (image != null)
{
ImageSource returnImage = image.Clone();
returnImage.Freeze();
return returnImage;
} else return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
显然这是不可能的,因为它看起来像是在 IValueConverter
有机会完成它的工作之前抛出异常......如果有人知道解决这个问题的替代方法不涉及迭代来源收集和设置所有图像冻结请让我知道。