DialogHost.Avalonia

repository·main·Indexed 18 days ago

https://github.com/avaloniautils/dialoghost.avalonia

A control for Avalonia applications providing an asynchronous way to display dialogs over existing content. It supports MVVM, code-behind, and pure XAML patterns, featuring a DialogHost content control, static API for showing dialogs, and support for multiple simultaneous dialogs via the IsMultipleDialogsEnabled property.

Tokens
1.8K
Snippets
7
Records
7
Agent score
13%

What's inside DialogHost.Avalonia

  1. Install and set up DialogHost.Avalonia

    main

    To use DialogHost.Avalonia, install the NuGet package and register its styles in your application.

    1. Install the package via dotnet CLI:

      dotnet add package DialogHost.Avalonia
    2. Add the DialogHostStyles to your App.axaml file.

    Note for versions below 0.7: Use StyleInclude instead of the DialogHostStyles element.

    dotnet add package DialogHost.Avalonia
  2. Implement the DialogHost control

    main

    The DialogHost is a content control that wraps your application content. It provides a DialogContent property where you define the UI for the popup. When the dialog is active, the content inside the DialogHost (but outside DialogContent) is dimmed and disabled.

    Set CloseOnClickAway="True" if you want the dialog to close when the user clicks outside the dialog area.

    <Window ...
            xmlns:dialogHost="clr-namespace:DialogHostAvalonia;assembly=DialogHost.Avalonia"
            Title="DialogHost.Demo">
        <dialogHost:DialogHost CloseOnClickAway="True">
            <dialogHost:DialogHost.DialogContent>
                <!-- Put your dialog content here -->
            </dialogHost:DialogHost.DialogContent>
            
            <!-- Put the content over which the dialog is shown here (e.g. your main window grid) -->
        </dialogHost:DialogHost>
    </Window>
  3. Configure DialogHostStyles in App.axaml

    main

    For modern versions, add the DialogHostStyles element within your Application.Styles. Ensure you include the correct namespace.

    For legacy versions (below 0.7), use a StyleInclude pointing to the internal Styles.xaml.

    <!-- Modern versions -->
    <Application ...
        xmlns:dialogHostAvalonia="clr-namespace:DialogHostAvalonia;assembly=DialogHost.Avalonia"
        ...>
        <Application.Styles>
            <dialogHostAvalonia:DialogHostStyles />
        </Application.Styles>
    </Application>
    
    <!-- Versions below 0.7 -->
    <StyleInclude Source="avares://DialogHost.Avalonia/Styles.xaml"/>
  4. Open a dialog using different strategies

    main

    You can trigger a dialog to open using XAML bindings, commands, or C# code.

    • XAML/Binding: Bind the IsOpen property to a boolean.
    • Commands: Use the OpenDialogCommand (available on the DialogHost instance) via RelativeSource binding. This is useful for buttons where you pass parameters via CommandParameter.
    • C# (Static API): Use DialogHost.Show(viewOrModel). This is an async/await based method suitable for ViewModels. If using multiple windows, you can specify a DialogHost.Identifier to target a specific instance.
    <!-- Using OpenDialogCommand on a button -->
    <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=dialogHost:DialogHost}, Path=OpenDialogCommand}" />
    
    <!-- Using IsOpen property -->
    <dialogHost:DialogHost IsOpen="True" />
    // Using the static Show API
    await DialogHost.Show(myContent);
    
    // Targeting a specific DialogHost by identifier
    await DialogHost.Show(myContent, "MyDialogIdentifier");
  5. Handle the DialogClosing event

    main

    The DialogClosingEventHandler allows you to intercept a close request, inspect the parameter passed to CloseDialogCommand, and potentially cancel the closing process.

    • Code-behind: Use the DialogClosing routed event.
    • MVVM: Bind to the DialogClosingCallback property.
    • Static API: Pass a delegate to the DialogHost.Show method.
    <!-- Code-behind approach -->
    <dialogHost:DialogHost DialogClosing="DialogHost_OnDialogClosing" />
    
    <!-- MVVM approach -->
    <dialogHost:DialogHost DialogClosingCallback="{Binding DialogClosingHandler}" />
    // Intercepting via the Show API
    var result = await DialogHost.Show(viewOrModel, (sender, args) => 
    {
        // Logic to allow or cancel closing
    });
  6. Close a dialog using different strategies

    main

    Dialogs can be closed by updating the IsOpen property, using commands, or via the DialogSession.

    • XAML/Binding: Set IsOpen="False".
    • Commands: Bind to the CloseDialogCommand on the DialogHost. The command parameter passed to this command will be returned as the result of the Show() call.
    • DialogSession: Access the session via the DialogOpenedEventArgs in an opened event, or by using DialogHost.GetDialogSession("Identifier") to close it programmatically.
    <!-- Using CloseDialogCommand inside the dialog content -->
    <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=dialogHost:DialogHost}, Path=CloseDialogCommand}" CommandParameter="Success" />
    // Closing via DialogOpenedEventArgs session
    var result = await DialogHost.Show(myContent, delegate(object sender, DialogOpenedEventArgs args)
    {
        args.Session.Close(false);
    });
    
    // Closing via Identifier
    DialogHost.GetDialogSession("MyDialogIdentifier")?.Close(false);