BitmapImage SetSource Crashing/Freezing App

我的未来我决定 提交于 2019-12-11 09:32:59

问题


I am trying to set the image from a stream. However when I set the source from the background thread and use a dispatcher it freezes the app completely. The stream is not null, I have verified that.

I am using the taglib api to get the stream of the album picture of an mp3 file. I've tried everything.

async void Background(object sender, MediaPlayerDataReceivedEventArgs e)
{         
   IRandomAccessStream AlbumArtStream = await Media.GetAlbumArt(MediaFile.Name, await MediaFile.OpenStreamForReadAsync());

   await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
   {
        ViewModel.NewAlbumArt = new BitmapImage();
        NewAlbumArt.SetSource(AlbumArtStream);
   }
}

回答1:


Like in comment - to prevent freezing your app, call SetSoruceAsync:

async void Background(object sender, MediaPlayerDataReceivedEventArgs e)
{         
   IRandomAccessStream AlbumArtStream = await Media.GetAlbumArt(MediaFile.Name, await MediaFile.OpenStreamForReadAsync());

   await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
   {
      ViewModel.NewAlbumArt = new BitmapImage();
      await NewAlbumArt.SetSourceAsync(AlbumArtStream);
   }
}

You may read more at MSDN:

Setting an image source by calling the asynchronous SetSourceAsync method rather than the similar SetSource method avoids blocking the UI thread. ...



来源:https://stackoverflow.com/questions/23925350/bitmapimage-setsource-crashing-freezing-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!