LoadCoverBitmapAsync
. This returns a stream that can be used to load a bitmap from.AlbumViewModel.cs
and add a property of type Bitmap?
named Cover
using the common pattern so that it can notify the UI when it changes. You will need a using Avalonia.Media.Imaging;
directive to do so.Bitmap.DecodeToWidth
method to create a Bitmap object. This is an important insight if you want to display images in your UI and the format (jpg, png, and otherwise) provides a very large high resolution image but you only want to display it in a small portion of your UI. You need to decode the image so that the Bitmap
that is displayed is not the full resolution. This DecodeToWidth
method maintains the aspect ratio and efficiently loads to the specified width. This means we will not waste huge amounts of memory to show our album covers even if the image files themselves turn out to be quite large.AlbumViewModel.cs
should now look like:MusicStoreViewModel.cs
and add the following method.SearchResults
and call our AlbumViewModel
s LoadCover
method. Creating a copy with .ToList()
is necessary because this method is async and SearchResults
might be updated by another thread.CancellationToken
is used to check if we want to stop loading album covers.DoSearch
method of MusicStoreViewModel
after the SearchResults.Clear();
line._cancellationTokenSource
might be replaced asynchronously we have to store the cancellation token in a local variable.DoSearch
method of MusicStoreViewModel
after the foreach
loop.DoSearch
should now look like this.