You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public partial AlbumViewModel? SelectedAlbum { get; set; }
1516
1516
```
1517
-
==== Implement the Album Existence Check in MainWindowViewModel
1518
-
The MainWindowViewModel will handle the `CheckAlbumAlreadyExistsMessage` to determine if the selected album is already in the user's collection. To do this, you will implement a message handler that checks the observable collection of albums for the presence of the selected album.
1517
+
==== Implement the Album Existence Check in MainViewModel
1518
+
The MainViewModel will handle the `CheckAlbumAlreadyExistsMessage` to determine if the selected album is already in the user's collection. To do this, you will implement a message handler that checks the observable collection of albums for the presence of the selected album.
1519
1519
1520
-
In the **MainWindowViewModel.cs** file, add the following code to the constructor:
1520
+
In the **MainViewModel.cs** file, add the following code to the constructor:
1521
1521
```csharp
1522
1522
public MainViewModel()
1523
1523
{
@@ -1572,8 +1572,6 @@ Your next step is bind the **Buy Album** button to the relay command in the musi
1572
1572
1573
1573
You will see the dialog close, but nothing happens in the main window!
1574
1574
1575
-
// Todo: Continue here...
1576
-
1577
1575
=== Add Items to the User's Collection
1578
1576
1579
1577
On this page you will implement a collection of albums that the user has selected using the search dialog and the **Buy Album** button, and display them in the main window.
@@ -1585,9 +1583,9 @@ Your first step here is to add an observable collection to the main window view
1585
1583
Follow this procedure:
1586
1584
1587
1585
- Stop the app if it is running.
1588
-
- Locate and open the **MainWindowViewModel.cs** file.
1586
+
- Locate and open the **MainViewModel.cs** file.
1589
1587
- Add an observable collection, as shown:
1590
-
1588
+
+
1591
1589
```csharp
1592
1590
public ObservableCollection<AlbumViewModel> Albums { get; } = new();
1593
1591
```
@@ -1596,7 +1594,7 @@ public ObservableCollection<AlbumViewModel> Albums { get; } = new();
1596
1594
1597
1595
Your next step is to alter the `AddAlbumAsync` command so that it adds the dialog return object (an `AlbumViewModel`) to the observable collection. Follow this procedure:
1598
1596
1599
-
- In the same **MainWindowViewModel.cs** file update the `AddAlbumAsync()` command method:
1597
+
- In the same **MainViewModel.cs** file update the `AddAlbumAsync()` command method:
1600
1598
1601
1599
```csharp
1602
1600
[RelayCommand]
@@ -1618,13 +1616,13 @@ To add the items control and its data template, follow this procedure:
1618
1616
1619
1617
- Locate and open the **MainWindow.axaml** file.
1620
1618
- Add the following namespace declaration to the `<Window>` element:
You will see the user's album collection building as you search and select. However, if you stop the app running and then start it again, the collection reverts to empty.
@@ -1668,7 +1667,7 @@ Follow this procedure to add persistence services (save and load) to the album m
1668
1667
- Stop the app if it is running.
1669
1668
- Locate and open the **Album.cs** file in the **/Models** folder.
1670
1669
- Add the code to implement save to disk, as shown:
var coverTasks = albums.Select(album => album.LoadCover());
1838
-
await Task.WhenAll(coverTasks);
1839
1837
}
1840
-
1841
1838
```
1842
1839
1843
1840
As you can see this method uses the business service to load the list of albums from the disk cache. It then transforms each data model (`Album` class) into a view model (`AlbumViewModel` class). After this all the album view models are added to the observable collection - this will instantly update the UI with the text data for the albums.
1844
1841
1845
-
You will notice that after the JSON album files are loaded, the second loop loads the cover art image files. This provides your user with visual feedback as quickly as possible (in the form of album tiles with text and the placeholder music note icon) about what albums are in the collection. The cover art is then loaded asynchronously. This ensures that the app remains responsive during the image loading process.
1846
-
1847
1842
Your next step is to schedule the `LoadAlbum` method to run when the app starts:
1848
1843
1849
-
- Keep the **MainWindowViewModel.cs** file open.
1850
-
- Call LoadAlbums() from the MainWindowViewModel constructor:
1844
+
- Keep the **MainViewModel.cs** file open.
1845
+
- Call LoadAlbums() from the MainViewModel constructor:
1851
1846
1852
1847
```csharp
1853
-
public MainWindowViewModel()
1848
+
public MainViewModel()
1854
1849
{
1855
1850
LoadAlbums();
1856
1851
}
@@ -1859,6 +1854,7 @@ With this change, now the app will automatically load previously added albums ev
1859
1854
1860
1855
- Click **Debug** to compile and run the project.
1861
1856
1857
+
.Well done, this is the final result of the Music Store App tutorial
0 commit comments