我怎样才能等到下载操作完成并想要将状态真或假返回给 UI。现在我正在使用 Webclient 下载这样的图像
private async Task SaveCoversAsync(string CoverImageUrl)
{
WebClient getImageClient = new WebClient();
getImageClient.OpenReadCompleted += new OpenReadCompletedEventHandler(getImageClient_OpenReadCompleted);
getImageClient.OpenReadAsync(new Uri(CoverImageUrl), CoverImageUrl);
}
private async void getImageClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
var storeFile = IsolatedStorageFile.GetUserStoreForApplication();
string coverpath = string.Concat("filename.png");
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(coverpath, System.IO.FileMode.Create, FileAccess.Write, FileShare.Write, storeFile))
{
byte[] buffer = new byte[1024];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
catch (Exception exe)
{
}
}
问题是我不能使用 await ,因为我正在使用 OpenReadCompleted 事件。如何使用 WebClient.DownloadFileAsync 转换上述代码块?或者有什么方法可以等到下载结束并返回状态