Add Items to Users Collection

Adding Albums to the Users Collection

Ok so now the user can find albums to purchase, it would be nice if the user could see which albums are in their collection. To do this we can add a similar UI to the MainWindow.
Modify the MainWindow.axaml so that it places the existing Button inside a panel. We can then add a TextBlock that we can show when the user has no music, to prompt them to buy some.
We can then use an ItemsControl instead of a ListBox as we did before. An ItemsControl is the exact same as a ListBox except it doesnt allow the user to select anything.
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:vm="using:Avalonia.MusicStore.ViewModels"
4
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
xmlns:local="clr-namespace:Avalonia.MusicStore.Views"
7
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
8
x:Class="Avalonia.MusicStore.Views.MainWindow"
9
Icon="/Assets/avalonia-logo.ico"
10
Title="Avalonia.MusicStore"
11
TransparencyLevelHint="AcrylicBlur"
12
Background="Transparent"
13
ExtendClientAreaToDecorationsHint="True"
14
WindowStartupLocation="CenterScreen">
15
​
16
<Design.DataContext>
17
<vm:MainWindowViewModel />
18
</Design.DataContext>
19
​
20
<Panel>
21
<ExperimentalAcrylicBorder IsHitTestVisible="False">
22
<ExperimentalAcrylicBorder.Material>
23
<ExperimentalAcrylicMaterial
24
BackgroundSource="Digger"
25
TintColor="Black"
26
TintOpacity="1"
27
MaterialOpacity="0.65" />
28
</ExperimentalAcrylicBorder.Material>
29
</ExperimentalAcrylicBorder>
30
​
31
<Panel Margin="40">
32
<Button HorizontalAlignment="Right" VerticalAlignment="Top" Command="{Binding BuyMusicCommand}">
33
<PathIcon Data="{StaticResource store_microsoft_regular}" />
34
</Button>
35
​
36
<TextBlock IsVisible="{Binding CollectionEmpty}" Text="Its lonely in here. Purchase some music to get your collection started." HorizontalAlignment="Center" VerticalAlignment="Center" />
37
​
38
<ItemsControl Margin="0 40 0 0" Items="{Binding Albums}">
39
<ItemsControl.ItemsPanel>
40
<ItemsPanelTemplate>
41
<WrapPanel />
42
</ItemsPanelTemplate>
43
</ItemsControl.ItemsPanel>
44
​
45
<ItemsControl.ItemTemplate>
46
<DataTemplate>
47
<local:AlbumView Margin="0 0 20 20" />
48
</DataTemplate>
49
</ItemsControl.ItemTemplate>
50
</ItemsControl>
51
</Panel>
52
</Panel>
53
</Window>
Copied!
Just like in MusicStoreWindow.axaml we have to add xmlns:local="clr-namespace:Avalonia.MusicStore.Views" to the Window properties in order to use our custom control.
Now open MainWindowViewModel and add the following properties.
1
private bool _collectionEmpty;
2
​
3
public bool CollectionEmpty
4
{
5
get => _collectionEmpty;
6
set => this.RaiseAndSetIfChanged(ref _collectionEmpty, value);
7
}
8
​
9
public ObservableCollection<AlbumViewModel> Albums { get; } = new();
Copied!
Notice again we use an ObservableCollection for the Albums property, and we add a simple CollectionEmpty property to tell us when the users music collection is empty.
In order to have the CollectionEmpty property updated we can add the following code to the constructor.
1
this.WhenAnyValue(x => x.Albums.Count)
2
.Subscribe(x => CollectionEmpty = x == 0);
Copied!
Then in the constructor we can modify the BuyMusicCommand so that it adds the purchased Album to the list of Albums.
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
});
Copied!
Notice we check the result for null, this is because the user may have cancelled the dialog without selecting anything, in which case nothing should be added. If the dialog returns a result then we simply add it to the list of Albums.
Lets run the program and see if it works.
image-20210310175949319
For the finishing touch we simply need to add persistance to the application.
Last modified 19d ago
Export as PDF
Copy link