如何将从URL(服务器)下载的图像保存到Windows应用商店应用程序中的本地文件夹
本文关键字:应用 Windows 应用程序 文件夹 图像 URL 服务器 下载 保存 | 更新日期: 2023-09-27 18:34:46
我正在尝试从服务器下载图像并将其保存在本地文件中。我试过了
private async void save()
{
Uri source = new Uri("http://www.google.ca/intl/en_com/images/srpr/logo1w.png");
StorageFile destinationFile;
try
{
destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"downloadimage.jpg", CreationCollisionOption.GenerateUniqueName);
}
catch (FileNotFoundException ex)
{
System.Diagnostics.Debug.WriteLine("bingooooo"+ex.ToString());
return;
}
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
await download.StartAsync();
ResponseInformation response = download.GetResponseInformation();
}
但它没有任何替代方案
你的代码很好,只是缺少Task
作为返回类型。如果方法是异步的,则应使用返回类型作为Task
而不是void
。如果返回类型为 string
并且方法是异步的,则返回类型应Task<string>
。
将private async void save()
更改为private async Task save()
。
调用save()
时附加await
关键字。
查看使用 WinRT 深入潜水并等待