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
- Check `DialogHost.IsDialogOpen(identifier)` (or `GetDialogSession`) before calling `Close`.
- Guard against double-close by disabling the close command after first invocation.
- Ensure the same `dialogIdentifier` is used for `Show` and `Close`.
- 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
- Check IsDialogOpen before Close.
- Use the same identifier for Show and Close.
- Disable close commands after first use to avoid double-close.
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
- No loaded DialogHost instances.
- This element has no adorner layer.
- visualAncestry must contains at least one Visual control ins
- content
- No loaded DialogHost have an {nameof(Identifier)} property m
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/82cf40cbb84e3cdc.
Report an issue: GitHub.