MahApps/MahApps.Metro · error · InvalidOperationException

The provided dialog is not visible in the specified window.

Error message

The provided dialog is not visible in the specified window.

What it means

Thrown by HideMetroDialogAsync when the dialog is in neither the active nor inactive container. This means ShowMetroDialogAsync was never called for that dialog, or it was already hidden/removed. MahApps.Metro refuses to hide a dialog it does not currently manage.

Source

Thrown at src/MahApps.Metro/Controls/Dialogs/DialogManager.cs:364

        /// This happens if <see cref="ShowMetroDialogAsync"/> hasn't been called before.
        /// </exception>
        public static async Task HideMetroDialogAsync(this MetroWindow window, BaseMetroDialog dialog, MetroDialogSettings? settings = null)
        {
            window.Dispatcher.VerifyAccess();

            if (window.metroActiveDialogContainer is null)
            {
                throw new InvalidOperationException("Active dialog container could not be found.");
            }

            if (window.metroInactiveDialogContainer is null)
            {
                throw new InvalidOperationException("Inactive dialog container could not be found.");
            }

            if (!window.metroActiveDialogContainer.Children.Contains(dialog) && !window.metroInactiveDialogContainer.Children.Contains(dialog))
            {
                throw new InvalidOperationException("The provided dialog is not visible in the specified window.");
            }

            // once a button as been clicked, begin removing the dialog.
            dialog.FireOnClose();

            DialogClosed?.Invoke(window, new DialogStateChangedEventArgs(dialog));

            await dialog.WaitForCloseAsync();

            window.SizeChanged -= dialog.SizeChangedHandler;
            window.RemoveDialog(dialog);

            settings ??= dialog.DialogSettings;
            await HandleOverlayOnHideAsync(settings, window);
        }

        /// <summary>
        /// Gets the current shown dialog in async way.

View on GitHub (pinned to 72099e310b)

Solutions

  1. Call HideMetroDialogAsync exactly once per successful ShowMetroDialogAsync, and track the shown state in a field.
  2. For built-in dialogs that self-close (ShowMessageAsync), do not call HideMetroDialogAsync afterward — await the returned task instead.
  3. Guard the hide call: only hide if the dialog is still considered open (track a boolean or check the controller/await result).
  4. Ensure you show and hide on the same MetroWindow instance.

Example fix

// before
await window.ShowMetroDialogAsync(dialog);
// ... user closes via dialog button which hides it ...
await window.HideMetroDialogAsync(dialog); // throws - already hidden

// after
await window.ShowMetroDialogAsync(dialog);
await dialog.WaitUntilUnloadedAsync();
// do not hide again; it is already removed
Defensive patterns

Strategy: validation

Validate before calling

// Track whether the dialog is currently shown by this window before hiding.
private bool _isShown;

async Task ShowHideAsync(MetroWindow window, BaseMetroDialog dialog)
{
    if (_isShown) return;
    _isShown = true;
    await window.ShowMetroDialogAsync(dialog);
    await window.HideMetroDialogAsync(dialog);
    _isShown = false;
}

// For built-in self-closing dialogs, await the task and do NOT call HideMetroDialogAsync.

Try / catch

try { await window.HideMetroDialogAsync(dialog); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not visible"))
{
    // Already hidden (e.g. self-closed). Safe to ignore.
}

Prevention

When it happens

Trigger: Calling HideMetroDialogAsync on a freshly constructed dialog never shown; hiding the same dialog twice (second call); showing via one MetroWindow and hiding via another; hiding after the dialog auto-closed (e.g. via its own close button).

Common situations: A message dialog's affirm/OK button already hides it internally, then user code also calls HideMetroDialogAsync; racing async close paths; reusing a dialog reference after it was disposed/hidden in an earlier flow.

Related errors


AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13). Data as JSON: /api/errors/4051b86e9abcbdc9. Report an issue: GitHub.