在Converter中显示StorageItem缩略图

时间:2014-09-01 04:33:10

标签: c# windows-phone-8 windows-runtime async-await windows-phone-8.1

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

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

但是转换器必须同步运行。那么无论如何都要这样做吗?

1 个答案:

答案 0 :(得分:3)

不要在 Converter 中运行 async 代码,而是在完成任务(GetThumbnail)时让绑定工作。 Here is a nice post from 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}}"/>