Skip to content

Dialogs

Tom Laird-McConnell edited this page Dec 21, 2024 · 8 revisions

Console apps don't have the ability to use a system window for model dialogs, so Consolonia implements them as character based pop-up windows.

DialogWindow control

The DialogWindow encapsulates all of the logic to create the popup window.

image

The base class for dialog windows is the DialogWindow class, so to define a dialog you create an element which derives from DialogWindow

<controls:DialogWindow xmlns="https://github.com/avaloniaui"
                       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                       xmlns:controls="clr-namespace:Consolonia.Core.Controls;assembly=Consolonia.Core"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       x:Class="Consolonia.Gallery.Gallery.GalleryViews.SomeDialogWindow">
    <Panel>
        <TextBlock Text="Hello World" />    
        <Button Content="DoSomething"
                Name="DoSomethingButton"
                HorizontalAlignment="Right"
                VerticalAlignment="Bottom"
                Click="DoSomethingButton_Clicked" />
    </Panel>
</controls:DialogWindow>

Show a Dialog

To show a dialog you create an instance of the dialog and call the .ShowDialogAsync() method on it.

var dialog = new SomeDialogWindow();
var result = async dialog.ShowDialogAsync<MyResult>();

Close a Dialog

To close the dialog you write a click handler which calls the Close method, optionally passing a result back to the caller.

Close(result);

Dialog Result

The result passed to .Close() is returned to the caller of ShowDialogAsync method. The result will be of type ResultT.

MessageBox control

image

Consolonia implements a DialogWindow called MessageBox which makes it simple to show simple message boxes and get input from the user.

Show a MessageBox

To show a message box you instantiate a MessageBox object and call the ShowDialogAsync method. The kind of buttons shown will depend on the mode that is passed in.

var mb = new MessageBox
{
    Mode = Mode.YesNoCancel,
    Title = "Yes/No/Cancel Message box"
};
var result = await MessageBox.ShowDialogAsync("Hello World");

MessageBox DialogMode

The dialog mode controls the buttons that are shown.

DialogMode Description
Ok Shows an OK button
OkCancel Shows an OK and Cancel button
YesNo Shows a Yes and No button
YesNoCancel Shows a Yes, No and Cancel button

MessageBoxResult

The result of the message box is of type MessageBoxResult and will be the result of the button clicked.

DialogBoxResult Description
Ok OK button was clicked
Cancel Cancel button was clicked
Yes Yes button was clicked
No No button was clicked

IStorage Dialogs

The Avalonia IStorage interface provides a way to interact with various storage mechanisms. In Consolonia it implements them using a DialogWindow providing character based way of letting the user interact with the file system.

The Avalonia IStorage interface is accessible on any Window component.

   // via Window
    IStorageProvider storageProvider = this.StorageProvider;

If you are a child control you can access the root window via the application lifetime global Application.Current.ApplicationLifetime.

if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
{
    IStorageProvider storageProvider = lifetime.MainWindow.StorageProvider;

.OpenFilePickerAsync()

image

The OpenFilePicker gives you the ability to ask the user to select one or more files.

    if (storageProvider.CanOpen)
    {
        var files = await storageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
        {
            Title = title,
            AllowMultiple = allowMultiple,
            SuggestedStartLocation = new SystemStorageFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))),
            FileTypeFilter = new List<FilePickerFileType>
            {
                new("All files") { Patterns = ["*"] },
                new("Text") { Patterns = ["*.txt"] },
                new("Comma Delimited Files") { Patterns = ["*.csv"] },
                new("PDF") { Patterns = ["*.pdf"] }
            }
        });
        // files is the selected files.
        ...

.OpenFolderPickerAsync()

image

The OpenFolderPickerAsync() method gives you the ability to ask your user to select a folder.

if (storageProvider.CanOpen)
{
   var folders = await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
                    {
                        Title = title,
                        SuggestedStartLocation = new SystemStorageFolder(
                            new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))),
                        AllowMultiple = allowMultiple
                    });
   ...

.SaveFilePickerAsync()

image

The .SaveFilePickerAsync() method gives you the ability ask the user to select the name and location to save a file.

  if (storageProvider.CanSave)
  {
      IStorageFile file = await storageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
      {
          Title = "Save File",
          SuggestedStartLocation =
              new SystemStorageFolder(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)),
          DefaultExtension = "txt",
          SuggestedFileName = "NewFile.txt",
          FileTypeChoices = new List<FilePickerFileType>
          {
              new("Text") { Patterns = ["*.txt"] },
              new("Comma Delimited Files") { Patterns = ["*.csv"] },
              new("PDF") { Patterns = ["*.pdf"] }
          }
      });