Open a Dialog
On this page you will learn how to open dialog window in your app and exchange data between windows using Mvvm.Messaging. The dialog will be used to search for and select an album to add to a list in the main window.
Two messages will be used in your app:
- PurchaseAlbumMessage — sent by the main view model to request the dialog window be shown and await a result.
- MusicStoreClosedMessage — sent by the dialog's view model when the user selects an album, to return the result and close the dialog.
Below is a diagram showing the message flow between the components that you are going to implement in the next steps:
Add a New Dialog Window
There is nothing special about a window view file that makes it into a dialog; that is up to the way in which the window is controlled by the app. You will use Avalonia UI features and CommunityToolkit.Mvvm to manage this. So the first step is to create a new window for the app.
To create a new window, follow this procedure:
- Stop the app if it is still running.
- In the solution explorer, right-click the /Views folder and then click Add.
- Click Avalonia Window.
- When prompted for the name, type 'MusicStoreWindow'
- Press enter.
Dialog Window Styling
To style the new dialog window so that it matches the main window, follow this procedure:
- Locate and open the MusicStoreWindow.axaml file.
- Change this code as follows to add the acrylic blur background, extended into the title bar (as before) as shown:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Avalonia.MusicStore.Views.MusicStoreWindow"
Title="MusicStoreWindow"
TransparencyLevelHint="AcrylicBlur"
ExtendClientAreaToDecorationsHint="True">
<Panel>
<ExperimentalAcrylicBorder IsHitTestVisible="False">
<ExperimentalAcrylicBorder.Material>
<ExperimentalAcrylicMaterial
BackgroundSource="Digger"
TintColor="Black"
TintOpacity="1"
MaterialOpacity="0.65" />
</ExperimentalAcrylicBorder.Material>
</ExperimentalAcrylicBorder>
<Panel Margin="40">
</Panel>
</Panel>
</Window>