我无法通过Windows应用商店下载文件。这是我的下载方法:
private static async void DownloadImage()
{
HttpClient client = new HttpClient();
HttpResponseMessage message = await client.GetAsync("http://coolvibe.com/wp-content/uploads/2010/05/scifiwallpaper1.jpg");
StorageFolder myfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = myfolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting).GetResults();// this line throws an exception
byte[] file = await message.Content.ReadAsByteArrayAsync();
await FileIO.WriteBytesAsync(sampleFile, file);
var files = await myfolder.GetFilesAsync();
}
在标记的行上,我得到此异常:
An exception of type 'System.InvalidOperationException' occurred in ListStalkerWin8.exe but was not handled in user code
WinRT information: A method was called at an unexpected time.
Additional information: A method was called at an unexpected time.
是什么原因造成的?
您正在尚未完成的IAsyncOperation上调用GetResults,因此不在可以访问结果的状态(因为它们尚不存在)。
实际上,您根本不需要调用GetResults,只需要:
StorageFile sampleFile = await myfolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句