MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

DialogHost is not open.

Error message

DialogHost is not open.

What it means

`DialogHost.Close(object? dialogIdentifier, object? parameter)` looks up the host instance and, if it has no active `CurrentSession`, throws `InvalidOperationException("DialogHost is not open.")`. It means you tried to close a dialog that was never opened (or was already closed) under that identifier.

Source

Thrown at src/MaterialDesignThemes.Wpf/DialogHost.cs:198

    /// </summary>
    /// <param name="dialogIdentifier"> of the instance where the dialog should be closed. Typically this will match an identifier set in XAML. </param>
    public static void Close(object? dialogIdentifier)
        => Close(dialogIdentifier, null);

    /// <summary>
    ///  Close a modal dialog.
    /// </summary>
    /// <param name="dialogIdentifier"> of the instance where the dialog should be closed. Typically this will match an identifier set in XAML. </param>
    /// <param name="parameter"> Value returned by DialogHost.ShowDialog(...) or passed to close handler if one is provided.</param>
    public static void Close(object? dialogIdentifier, object? parameter)
    {
        DialogHost dialogHost = GetInstance(dialogIdentifier);
        if (dialogHost.CurrentSession is { } currentSession)
        {
            currentSession.Close(parameter);
            return;
        }
        throw new InvalidOperationException("DialogHost is not open.");
    }

    /// <summary>
    /// Retrieve the current dialog session for a DialogHost
    /// </summary>
    /// <param name="dialogIdentifier">The identifier to use to retrieve the DialogHost</param>
    /// <returns>The DialogSession if one is in process, or null</returns>
    public static DialogSession? GetDialogSession(object? dialogIdentifier)
    {
        DialogHost dialogHost = GetInstance(dialogIdentifier);
        return dialogHost.CurrentSession;
    }

    /// <summary>
    /// dialog instance exists
    /// </summary>
    /// <param name="dialogIdentifier">of the instance where the dialog should be closed. Typically this will match an identifier set in XAML.</param>
    /// <returns></returns>

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Check `DialogHost.IsDialogOpen(identifier)` (or `GetDialogSession`) before calling `Close`.
  2. Guard against double-close by disabling the close command after first invocation.
  3. Ensure the same `dialogIdentifier` is used for `Show` and `Close`.
  4. Catch `InvalidOperationException` around programmatic close if the dialog may already be gone.

Example fix

// before
DialogHost.Close("RootDialog"); // throws if nothing is open

// after
if (DialogHost.IsDialogOpen("RootDialog"))
    DialogHost.Close("RootDialog");
Defensive patterns

Strategy: validation

Validate before calling

if (DialogHost.IsDialogOpen(DialogIds.Root))
    DialogHost.Close(DialogIds.Root);

Type guard

static bool CanClose(object identifier) => DialogHost.IsDialogOpen(identifier);

Try / catch

try { DialogHost.Close(DialogIds.Root); }
catch (InvalidOperationException) { /* already closed: ignore */ }

Prevention

When it happens

Trigger: Calling `DialogHost.Close(identifier)` when no dialog is currently shown for that identifier; double-closing; closing after the dialog already ended; closing before `Show` was called.

Common situations: Race between a close command and the dialog not yet opened; close handlers firing twice; closing on a different identifier than the one used to show; closing after the user already dismissed the dialog.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/82cf40cbb84e3cdc. Report an issue: GitHub.