我目前已经为数据绑定的WPF图像实现了这个功能:
<Image Source="{Binding ThumbFile}" />
简单到足以应对。
现在,将缓存添加到这个图像中(我希望能够在加载完毕后操纵/删除本地文件)。我发现可以在 <img>
标签内添加 CacheOption="OnLoad" 来实现。
<Image>
<Image.Source>
<BitmapImage UriSource="{Binding Path=ThumbFile, Converter={StaticResource myConverter2}}" />
</Image.Source>
</Image>
然后我需要一个转换器将本地文件转换为BitmapImage。
<local:LocalUriToImageConverter x:Key="myConverter2"/>
并且
public class LocalUriToImageConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
{
return null;
}
if (value is string)
{
value = new Uri((string)value);
}
if (value is Uri)
{
System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
bi.BeginInit();
//bi.DecodePixelWidth = 80;
bi.DecodePixelHeight = 60;
bi.UriSource = (Uri)value;
bi.EndInit();
return bi;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
由于某些原因,这根本没有开始工作。没有错误,但是似乎无法绑定控件。即使创建了许多实例,缩略图文件属性的get和转换器中的断点也不会被触发。切换回其他图像源标记可以正常工作。