Search code examples
c#windows-runtimewinrt-async

Calling await file.OpenAsync appears to hang


I've got a property that is bound in XAML, where the property is supposed to return an image from a file. The property calls the following code:

private async Task<BitmapImage> GetBitmapImageAsync(StorageFile file)
{
   Debug.WriteLine("GetBitmapImageAsync for file {0}", file.Path);
   BitmapImage bitmap = new BitmapImage();
   Debug.WriteLine("... opening the stream");
   using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
   {
      Debug.WriteLine("... setting the source");
      bitmap.SetSource(stream);
      Debug.WriteLine("... and returning");
      return bitmap;
   }
}

The problem I've got is that the code will output the debugging text "... opening the stream" and then it appears to hang.

Can anyone see what I've done wrong or what I can try to fix this?


Solution

  • I had a similiar problem: WinRT: Loading static data with GetFileFromApplicationUriAsync()

    Please look at the answer from Alexander.