2013-08-08 4 views
4

Windows 스토어 앱을 통해 파일을 다운로드하는 데 문제가 있습니다. 이 원인이 될 수있는 어떤Windows 스토어 앱에서 파일 다운로드

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. 

:이 예외가 표시된 줄에

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(); 

    } 

: 여기 다운로드 내 방법? 당신은 IAsyncOperation에 GetResults를 호출하고

답변

2

아직 완료되지 않은 그 이유

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

내가 모르는 불구하고, 문제를 해결하는 것

+0

thnku very much Mario Stoilov와 Chris W.이 문제로 침몰하고있었습니다. – FebinDonz

0

변경

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

에 , 결과에 액세스 할 수있는 상태가 아닙니다 (아직 존재하지 않기 때문에). CreateFileAsync() 함수는 비동기이기 때문에

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

그것은 문제를 해결

는 사실, 당신은 당신은 단지 필요가 전혀 GetResults에 대한 호출이 필요하지 않습니다. Windows Store C# API (WinJS에 대해 몰라서)에서 비동기 함수는 "비동기"접미사를 갖습니다. 즉, 파일 생성을 시작하고 다른 작업을 수행 한 다음 나중에 StorageFile을 검색 할 수 있습니다. "await"키워드를 추가하면 함수가 동기가됩니다. 그것은 "파일 작성을 시작하고 완료 될 때까지 다시 오지 말라"는 것과 같습니다. 나는 길지만 계산대라는 것을 알고있다. http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 – YasharBahman

관련 문제