c# - 在 Converter 中显示 StorageItem 缩略图

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

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

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

但是转换器必须同步运行。那么有没有办法做到这一点?

最佳答案

不要在 Converter 中运行async 代码,而是让您的绑定(bind)在任务 (GetThumbnail) 完成时工作。 Here is a nice post from Stephen Cleary关于异步 MVVM 模式 - 应用程序:数据绑定(bind)

您会发现一个我认为您可以使用的类 - 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}}"/>

关于c# - 在 Converter 中显示 StorageItem 缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25598874/

相关文章:

windows - 我可以将 winRT 应用程序作为屏幕保护程序运行吗?

c# - Entity Framework 、存储库模式、工作单元和测试

c# - 正则表达式与阿拉伯语匹配

windows-phone-8 - WP MVVM 导航 OnNavigatedTO

c# - 将应用程序部署到 Windows Phone 时出错

javascript - 如何在 Metro 应用程序中存储用户偏好

c# - 亚马逊产品广告 API ItemSearch 返回空值

c# - Xamarin列表中的元素重叠

c# - 当声音仍在播放时,Windows Phone 8 上的 XNA 会循环播放音频

windows-runtime - ListView 重新排序是否在 Windows 10 RT 应用程序上停止工作?