[英]Loading image asynchronously in WPF using binding converter
我有一個具有綁定設置的Image控件,如下所示:
<Image Stretch="Uniform" Source="{Binding Path=CurrentItem, Converter={StaticResource ImgConverter}, IsAsync=True}"/>
ImgConverter
是:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string uri = null;
Compressor.Unzip((value as Zipfile));
uri = string.Format("{0}{1}.jpg", Compressor.TempPath, value.ToString());
return uri;
}
Compressor.Unzip(...)
方法確實需要一些時間。 我設置了綁定IsAsync=True
但是它不起作用(不是在轉換器上而是僅在路徑上?)。 我該如何異步處理?
在您的Zipfile類中添加一個只讀Image
屬性,並將轉換器代碼移至屬性getter:
public class Zipfile
{
...
public ImageSource Image
{
get
{
Compressor.Unzip(this);
var uri = string.Format("{0}{1}.jpg", Compressor.TempPath, this.ToString());
return new BitmapImage(new Uri(uri));
}
}
}
然后像這樣寫你的綁定:
<Image Source="{Binding Path=CurrentItem.Image, IsAsync=True}"/>
將Multibinding與兩個綁定一起使用,第一個是自我圖像控制,第二個是您當前的綁定,在轉換器中,您可以調用async您的方法來解壓縮文件,而在回調中,您可以設置圖像路徑(請參閱第一個綁定); 您還可以看到以下示例:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.