LoadCoverBitmapAsyncthis returns a stream that can be used to load a bitmap from.
AlbumViewModel.csand add a property of type
Coverusing 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.DecodeToWidthmethod to create a Bitmap object. This is an important insight, if you want to display images in your UI and the jpg, png or whatever format you using provides a very large hight 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
Bitmapthis is displayed is not the full resolution. This
DecodeToWidthmethod maintains the aspect ratio and loads to the specified width, efficiently. 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.csshould now look like:
MusicStoreViewModel.csand add the following method.
SearchResultsand call our
LoadCovermethod. Creating a copy with
.ToList()is necessary because this method is async and
SearchResultsmight be updated by another thread.
CancellationTokenis used to check if we want to stop loading album covers.
_cancellationTokenSourcemight be replaced asynchronously we have to store the cancellation token in a local variable.
DoSearchshould now look like this.