3

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.

何が原因でしょうか?

4

2 に答える 2

2

まだ完了していない IAsyncOperation で GetResults を呼び出しているため、結果にアクセスできる状態ではありません (まだ存在しないため)。

実際、GetResults の呼び出しはまったく必要ありません。必要なのは次のとおりです。

StorageFile sampleFile = await myfolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting);
于 2013-08-14T04:11:04.903 に答える
0

変化

StorageFile sampleFile = myfolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting).GetResults();//

StorageFile sampleFile = await myfolder.CreateFileAsync("image.jpg", CreationCollisionOption.ReplaceExisting);

理由はわかりませんが、問題は解決したようです

于 2013-08-08T10:53:09.223 に答える