MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

No loaded DialogHost instances.

Error message

No loaded DialogHost instances.

What it means

`DialogHost.GetInstance(dialogIdentifier)` resolves the active host from the static `LoadedInstances` weak-reference list. If no `DialogHost` is currently loaded in any visual tree, it throws `InvalidOperationException("No loaded DialogHost instances.")` before considering the identifier.

Source

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

    /// <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>
    public static bool IsDialogOpen(object? dialogIdentifier) => GetDialogSession(dialogIdentifier)?.IsEnded == false;

    private static DialogHost GetInstance(object? dialogIdentifier)
    {
        if (LoadedInstances.Count == 0)
            throw new InvalidOperationException("No loaded DialogHost instances.");

        List<DialogHost> targets = [];
        foreach (var instance in LoadedInstances.ToList())
        {
            if (instance.TryGetTarget(out DialogHost? dialogInstance))
            {
                object? identifier = null;
                if (dialogInstance.CheckAccess())
                {
                    identifier = dialogInstance.Identifier;
                }
                else
                {
                    // Retrieve the identifier using the Dispatcher on the owning thread (effectively replaces the VerifyAccess() call previously used)
                    identifier = dialogInstance.Dispatcher.Invoke(() => dialogInstance.Identifier);
                }
                if (Equals(dialogIdentifier, identifier))
                {

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Ensure at least one `<materialDesign:DialogHost Identifier="..." />` is present in a live visual tree (typically near the window root) before calling Show/Close.
  2. Defer dialog calls until the host's `Loaded` event (or the window's `OnContentRendered`).
  3. Keep the host alive for the lifetime you intend to show dialogs (e.g. on the main window, not a transient view).
  4. Verify with `DialogHost.IsDialogOpen`/a custom check that a host instance is loaded.

Example fix

<!-- before: no DialogHost in the window -> Show throws 'No loaded DialogHost instances.' -->
<Window> ... <!-- no DialogHost --> </Window>

<!-- after -->
<Window>
  <materialDesign:DialogHost Identifier="RootDialog" />
</Window>
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a DialogHost is loaded before any Show/Close:
// place <materialDesign:DialogHost Identifier="..."/> in the window,
// then defer calls until Loaded:
host.Loaded += async (s, e) => await DialogHost.Show(content, id);

Try / catch

try { await DialogHost.Show(content, id); }
catch (InvalidOperationException ex) when (ex.Message.Contains("No loaded DialogHost"))
{ /* show inline fallback / log: no host mounted */ }

Prevention

When it happens

Trigger: Calling `DialogHost.Show`/`Close` before any `<materialDesign:DialogHost>` has been loaded, or after all host instances were unloaded (window closed) and their weak references collected.

Common situations: Forgetting to place a `DialogHost` in the window; calling Show from a non-UI context or before the window's `Loaded`; the host living in a window that was closed; dialog opened from a service that runs at startup before UI is up.

Related errors


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