Add Data Persistence

Persisting Music Collection

Persistence is the job of the business logic (model) code. However here we will see it's very simple to wire this up to the UI.
Add the following method to AlbumViewModel this will call our backend logic to save the Album to the users collection. The backend also provides a way to cache the album bitmap. It provides us a stream we can save our bitmap to.
1
public async Task SaveToDiskAsync()
2
{
3
await _album.SaveAsync();
4
​
5
if (Cover != null)
6
{
7
var bitmap = Cover;
8
​
9
await Task.Run(() =>
10
{
11
using (var fs = _album.SaveCoverBitmapSteam())
12
{
13
bitmap.Save(fs);
14
}
15
});
16
}
17
}
Copied!
Now we can simply call this method when our dialog returns its result in MainWindowViewModel.cs.
1
BuyMusicCommand = ReactiveCommand.CreateFromTask(async () =>
2
{
3
var store = new MusicStoreViewModel();
4
​
5
var result = await ShowDialog.Handle(store);
6
​
7
if (result != null)
8
{
9
Albums.Add(result);
10
​
11
await result.SaveToDiskAsync();
12
}
13
});
Copied!
Last modified 1mo ago
Export as PDF
Copy link