我是C#的新手,仍然试图了解异步方法的工作原理。我的UWP应用程序需要在压缩文件夹放入屏幕时从压缩文件夹中检索缩略图JPG,在文件夹上传时显示带有进度环的缩略图,然后在上传完成后删除进度环。
首先,当用户删除文件时会触发此方法:
private async void OnFileDrop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var items = await e.DataView.GetStorageItemsAsync();
if (items.Count > 0)
{
foreach (var appFile in items.OfType<StorageFile>())
{
StorageFolder downloadFolder = ApplicationData.Current.LocalFolder;
StorageFolder unzipFolder =
await downloadFolder.CreateFolderAsync(Path.GetFileNameWithoutExtension(appFile.Name),
CreationCollisionOption.GenerateUniqueName);
await UnZipFileAsync(appFile, unzipFolder);
}
}
}
下一步:
public static IAsyncAction UnZipFileAsync(StorageFile zipFile, StorageFolder destinationFolder, Action<ZipArchiveEntry, StorageFolder> callback, Action<ZipArchiveEntry> completeCallback)
{
return UnZipFileHelper(zipFile, destinationFolder, thumbnailCallback, completeCallback).AsAsyncAction();
}
然后,此任务解压缩文件,在创建ZipArchive后调用thumbnailCallback方法:
private static async Task UnZipFileHelper(StorageFile zipFile, StorageFolder destinationFolder, Action<ZipArchiveEntry, StorageFolder> thumbnailCallback, Action<ZipArchiveEntry> completeCallback)
{
if (zipFile == null || destinationFolder == null ||
!Path.GetExtension(zipFile.Name).Equals(".zip", StringComparison.OrdinalIgnoreCase)
)
{
throw new ArgumentException("Invalid argument...");
}
Stream zipMemoryStream = await zipFile.OpenStreamForReadAsync();
// Create zip archive to access compressed files in memory stream
using (ZipArchive zipArchive = new ZipArchive(zipMemoryStream, ZipArchiveMode.Read))
{
ZipArchiveEntry thumbnail = zipArchive.GetEntry("thumbnail.jpg");
thumbnailCallback(thumbnail, destinationFolder);
// Unzip compressed file iteratively.
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{
await UnzipZipArchiveEntryAsync(entry, entry.FullName, destinationFolder);
}
}
}
这是thumbnailCallback方法,假设在上传文件夹时显示缩略图:
public async void thumbnailCallback(ZipArchiveEntry thumbnail, StorageFolder destinationFolder)
{
// thumbnail only displays after this has been called and user clicks OK button to close dialog
var messageDialog = new MessageDialog("displaying thumbnail");
await messageDialog.ShowAsync();
// code to display thumbnail
Canvas canvas = new Canvas();
canvas.Width = 200;
canvas.Height = 125;
ProgressRing progressRing = new ProgressRing();
progressRing.Name = thumbnail.FullName;
progressRing.IsActive = true;
progressRing.Height = 50;
progressRing.Width = 50;
Canvas.SetTop(progressRing, 35);
Canvas.SetLeft(progressRing, 75);
Canvas.SetZIndex(progressRing, 2);
Image thumb = new Image();
thumb.Name = thumbnail.FullName;
thumb.Width = 200;
thumb.Height = 125;
thumb.Opacity = 0.2;
BitmapImage bitmapImage = new BitmapImage();
Uri uri = new Uri(destinationFolder.Path + "\\" + thumbnail.FullName);
bitmapImage.UriSource = uri;
thumb.Source = bitmapImage;
canvas.Children.Add(thumb);
canvas.Children.Add(progressRing);
}
现在,只有首先调用MessageDialog.ShowAsync()才会显示缩略图,并且只有在对话框上单击“确定”按钮后才会显示缩略图。
答案 0 :(得分:1)
thumbnailCallback
的情况下调用 await
。这就是没有显示thumnail的原因(如果你很幸运,你可能会随机获得缩略图:))。放置MessageDialog后,线程就有足够的时间在用户交互后执行。
如何修复
如下所示:
await thumbnailCallback(thumbnail, destinationFolder);
建议:
将签名更改为
public async Task thumbnailCallback(ZipArchiveEntry thumbnail, StorageFolder destinationFolder)
通常,您需要返回Task
。主要的例外应该是当你需要一个void返回类型(对于事件)。
async
方法在另一方面是特殊的:它们代表顶级async
操作,并且当您的任务返回exception
时,还有其他规则可以发挥作用。< / p>