question

DeadStack avatar image
0 Votes"
DeadStack asked ·

await bitmapImage.SetSourceAsync(randomAccessStream); hangs and kills Xaml app.

Related to: strange-fileloadexceptions.html ( I edited the linked post because this turned out to be unrelated, just an issue with my usage)

When I call SetSourceAsync on a BitmapImage, it never completes and the Xaml app hangs.

Here's a capture of the random access stream feeding into the call. It looks valid.

5901-annotation-2020-03-25-204118.jpg

uwpuwp-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered ·

Hi, your problem is the use of Task<T> and return type. Try following demo.

XAML

 <Page
     x:Class="App1.Page04"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="using:App04"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d"
     Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   <Page.DataContext>
     <local:ViewModel/>
   </Page.DataContext>
   <Grid>
     <Image Source="{Binding Picture}"/>
   </Grid>
 </Page>

And ViewModel:

 using System;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
 using System.Threading.Tasks;
 using Windows.ApplicationModel;
 using Windows.Storage;
 using Windows.UI.Xaml.Controls;
 using Windows.UI.Xaml.Media;
 using Windows.UI.Xaml.Media.Imaging;
 
 namespace App04
 {
   public class ViewModel : INotifyPropertyChanged
   {
     public ViewModel() => GetPicture();
 
     public ImageSource Picture { get; set; }
     private async void GetPicture()
     {
       string filePath = "Assets\\DwarfPortrait.png";
       StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(filePath);
       Picture = await GetImageSourceFromStorageFile(file);
       OnPropertyChanged(nameof(Picture));
     }
 
     private static async Task<ImageSource> GetImageSourceFromStorageFile(StorageFile sf)
     {
       using (var randomAccessStream = await sf.OpenAsync(FileAccessMode.Read))
       {
         var result = new BitmapImage();
         await result.SetSourceAsync(randomAccessStream);
         return result;
       }
     }
 
     public event PropertyChangedEventHandler PropertyChanged;
     internal void OnPropertyChanged([CallerMemberName] string propName = "") =>
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
   }
 }

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. I missed that type mismatch, I don't have a full understanding of async usage, so it's easy for me to get into trouble.

0 Votes 0 · ·