Opening a Dialog

Opening a Dialog

Opening a dialog is an advanced topic, if you are very new to Avalonia, try not to get too stuck on this section, you may want to just copy in the code and move on. Then come back once you have a better understanding of some of the basics.
In this section we shall make it so that clicking the Store Button opens a modal dialog where the user can search for albums to buy.
First we need to add a Window to the project, right click on the Views folder and select Add β†’ Avalonia Window.
When prompted name this MusicStoreWindow and press the Enter key.
add-window
This will add the following code:
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6
x:Class="Avalonia.MusicStore.Views.MusicStoreWindow"
7
Title="MusicStoreWindow">
8
Welcome to Avalonia!
9
</Window>
Copied!
Change this code as follows to enable the Acrylic and extended client area so the Window will look like our MainWindow.
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6
x:Class="Avalonia.MusicStore.Views.MusicStoreWindow"
7
Title="MusicStoreWindow"
8
TransparencyLevelHint="AcrylicBlur"
9
ExtendClientAreaToDecorationsHint="True">
10
<Panel>
11
<ExperimentalAcrylicBorder IsHitTestVisible="False">
12
<ExperimentalAcrylicBorder.Material>
13
<ExperimentalAcrylicMaterial
14
BackgroundSource="Digger"
15
TintColor="Black"
16
TintOpacity="1"
17
MaterialOpacity="0.65" />
18
</ExperimentalAcrylicBorder.Material>
19
</ExperimentalAcrylicBorder>
20
​
21
<Panel Margin="40">
22
​
23
</Panel>
24
</Panel>
25
</Window>
Copied!
Now lets see how we can open this Window.
Avalonia comes with the ReactiveUI - An advanced, composable, reactive model-view-viewmodel framework. Other MVVM frameworks are available and you can work without one if you wish.
ReactiveUI makes it very easy to get started and provides a lot of functionality out of the box.
In order that we can open the dialog from the ViewModel, we will use what is called an interaction (ReactiveUI - Interactions)
Firstly right click on your ViewModel folder and Add β†’ Class / Interface the following class names:
MusicStoreViewModel, AlbumViewModel
we will leave the code as it is for now.
Now open MainWindowViewModel.cs and add the following code:
1
public Interaction<MusicStoreViewModel, AlbumViewModel?> ShowDialog { get; }
Copied!
The constructor will also change so that it instantiates the ShowDialog interaction. Notice that the System.Reactive.Linq namespace is added to the top and the BuyMusicCommand is changed to use CreateFromTask.
1
using System;
2
using System.Collections.Generic;
3
using System.Reactive.Linq;
4
using System.Text;
5
using System.Windows.Input;
6
using ReactiveUI;
7
​
8
namespace Avalonia.MusicStore.ViewModels
9
{
10
public class MainWindowViewModel : ViewModelBase
11
{
12
public MainWindowViewModel()
13
{
14
ShowDialog = new Interaction<MusicStoreViewModel, AlbumViewModel?>();
15
​
16
BuyMusicCommand = ReactiveCommand.CreateFromTask(async () =>
17
{
18
var store = new MusicStoreViewModel();
19
​
20
var result = await ShowDialog.Handle(store);
21
});
22
}
23
​
24
public ICommand BuyMusicCommand { get; }
25
​
26
public Interaction<MusicStoreViewModel, AlbumViewModel?> ShowDialog { get; }
27
}
28
}
Copied!
This still wont do much because some plumbing code is required in the so called "code behind".
Open MainWindow.xaml.cs you may need to expand the MainWindow.axaml file in to see this file.
  • Make the MainWindow class inherit ReactiveWindow<MainWindowViewModel> instead of just Window.
    This allows ReactiveUI to help us out a little bit. It also shows that this View knows about its ViewModel, this is allowed. Check the MVVM section above.
  • Add a DoShowDialogAsync method like so:
1
private async Task DoShowDialogAsync(InteractionContext<MusicStoreViewModel, AlbumViewModel?> interaction)
2
{
3
var dialog = new MusicStoreWindow();
4
dialog.DataContext = interaction.Input;
5
​
6
var result = await dialog.ShowDialog<AlbumViewModel?>(this);
7
interaction.SetOutput(result);
8
}
Copied!
This method creates the MusicStoreWindow, it sets its DataContext to interaction.Input, which is the instance of the ViewModel.
It then calls await dialog.ShowDialog<AlbumViewModel?>(this) on the dialog, passing this as an argument, showing that the MainWindow currently represented by this pointer will be the owner or parent window.
The code will stay awaiting asynchronously until the viewmodel closes the dialog and sets a result on the interaction.
Once the dialog has closed, it will return the result, which will be of type AlbumViewModel or null if the dialog is cancelled.
interaction.SetOutput (result) is then called. This causes the interaction to end, and the program will return to our MainWindowViewModel where we left of at the call to var result = await ShowDialog.Handle(store);.
  • Add the following WhenActivated call to the Windows constructor.
1
this.WhenActivated(d => d(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync)));
Copied!
d is an Action that takes a disposable, this means that ReactiveUI will clean up any subscriptions when this View is not on the screen for us.
Our entire MainWindow.xaml.cs should now look like:
1
using System.Threading.Tasks;
2
using Avalonia;
3
using Avalonia.Controls;
4
using Avalonia.Markup.Xaml;
5
using Avalonia.MusicStore.ViewModels;
6
using Avalonia.ReactiveUI;
7
using ReactiveUI;
8
​
9
namespace Avalonia.MusicStore.Views
10
{
11
public class MainWindow : ReactiveWindow<MainWindowViewModel>
12
{
13
public MainWindow()
14
{
15
InitializeComponent();
16
#if DEBUG
17
this.AttachDevTools();
18
#endif
19
this.WhenActivated(d => d(ViewModel!.ShowDialog.RegisterHandler(DoShowDialogAsync)));
20
}
21
​
22
private async Task DoShowDialogAsync(InteractionContext<MusicStoreViewModel, AlbumViewModel?> interaction)
23
{
24
var dialog = new MusicStoreWindow();
25
dialog.DataContext = interaction.Input;
26
​
27
var result = await dialog.ShowDialog<AlbumViewModel?>(this);
28
interaction.SetOutput(result);
29
}
30
​
31
private void InitializeComponent()
32
{
33
AvaloniaXamlLoader.Load(this);
34
}
35
}
36
}
Copied!
Now open MainWindow.axaml and set the property WindowStartupLocation="CenterScreen"on the <Window> element.
Open MusicStoreWindow.axaml and set WindowStartupLocation="CenterOwner" on the <Window> element.
These properties mean that the MainWindow will be positioned in the center of the users monitor when the program loads, and that the dialog window will be positioned in the center of the MainWindow that owns it.
Also set Width and Height properties of the MusicStoreWindows <Window> element to 1000 and 550 respectively.
Now run the application and click the Store button.
dialog-opened
As you can see the dialog window is opened perfectly centered inside the MainWindow.
Last modified 19d ago
Export as PDF
Copy link