【问题标题】:Display StorageItem thumbnail in Converter在 Converter 中显示 StorageItem 缩略图
【发布时间】:2014-09-01 13:17:24
【问题描述】:
在 Windows Phone 8.1 Runtime 中,我们只能通过 GetThumnailAsync() 方法异步获取 StorageItem 的缩略图。
我正在尝试显示特定文件夹中的文件列表,其中为转换器中列表中的每个项目设置了缩略图。
但是转换器必须同步运行。那么有没有办法做到这一点呢?
【问题讨论】:
标签:
c#
windows-phone-8
windows-runtime
async-await
windows-phone-8.1
【解决方案1】:
不要在 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}}"/>