ViewModel
that called the dialog.MusicStoreViewModel
has a SelectedAlbum
property that we added previously and that the ListBox
on the MusicStoreView
has its SelectedItem
property bound to this SelectedAlbum
property of the view model.SelectedAlbum
property will be kept in sync and set to the AlbumViewModel
instance that represents the SelectedItem
of the ListBox
.Button
of the MusicStoreView
has its Command
property bound to BuyMusicCommand
. This doesn't exist yet so lets add this to MusicStoreViewModel
with the following code.ReactiveCommand
this is where we are using ReactiveUI to provide some of the plumbing for us. Avalonia expects commands to be of type ICommand
and ReactiveCommand
implements this interface.ReactiveCommand<TParam, TResult>
has some type arguments. Commands can take a parameter, however we do not need a paramter in this case, so we use Unit
which is kind of a dummy type, it contains no data. Reactive Commands can also return a result. This will be useful for returning the Album the user wants to buy.MusicStoreViewModel
in order to instantiate the command and implement the code needed to return a result from the dialog:SelectedAlbum
.MusicStoreWindow.axaml.cs
and make a few changes.ReactiveWindow<MusicStoreViewModel>
so that ReactiveUI can help us out.d
is an action that we can pass a disposable to, so things can be unsubscribed when the Window is no longer active.Subscription
to our ViewModel
s BuyMusicCommand
. We subscribe it directly to the Window
s Close
method. A using System;
directive is required for this step.BuyMusicCommand
will be passed to the Close
method. Causing the Window to close and its result to be returned from the ShowDialog
call we made in the MainWindow.xaml.cs
code.MusicStoreWindow.xaml.cs
should now look like this.MainWindowViewModel
inside