BitmapImage SetSource 崩溃/冻结应用程序 [英] BitmapImage SetSource Crashing/Freezing App

查看:46
本文介绍了BitmapImage SetSource 崩溃/冻结应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从流中设置图像.但是,当我从后台线程设置源并使用调度程序时,它会完全冻结应用程序.流不为空,我已经验证过.

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.

我正在使用 taglib api 来获取 mp3 文件的专辑图片流.我什么都试过了.

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

推荐答案

喜欢在评论中 - 为了防止冻结您的应用程序,请调用 SetSoruceAsync:

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

您可以阅读更多在 MSDN:

通过调用异步 SetSourceAsync 方法而不是类似的 SetSource 方法来设置图像源可以避免阻塞 UI 线程....

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

这篇关于BitmapImage SetSource 崩溃/冻结应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆