在Converter中显示StorageItem缩略图

本文关键字:略图 StorageItem 显示 Converter | 更新日期: 2023-09-27 18:20:20

在Windows Phone 8.1运行时中,我们只能使用GetThumnailAsync()方法异步获取StorageItem的缩略图。

我正在尝试显示特定文件夹中的文件列表,并在转换器中为列表中的每个项目设置缩略图。

但是转换器必须同步运行。那么,有没有办法这样做呢?

在Converter中显示StorageItem缩略图

不要在Converter中运行异步代码,而是在任务(GetThumbnail)完成时让绑定工作。这是Stephen Cleary关于异步MVVM模式-应用程序:数据绑定的一篇很好的文章。

你会发现有一个我认为你可以使用的类-NotifyTaskCompletion。在代码中定义:

public NotifyTaskCompletion<BitmapImage> MyThumbnail { get; set; }
// run somewhere your async job:
MyThumbnail = NotifyTaskCompletion<BitmapImage>(file.GetThumnailAsync());

然后在xaml中,您肯定可以使用一个转换器,该转换器将在任务返回结果后立即运行:

<Image Source="{Binding MyThumbnail.Result}" Visibility="{Binding
  MyThumbnail.IsSuccessfullyCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>