问题
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