{"record":{"id":"4051b86e9abcbdc9","repo":"MahApps/MahApps.Metro","slug":"the-provided-dialog-is-not-visible-in-the-specifie","errorCode":null,"errorMessage":"The provided dialog is not visible in the specified window.","messagePattern":"The provided dialog is not visible in the specified window\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Controls/Dialogs/DialogManager.cs","lineNumber":364,"sourceCode":"        /// This happens if <see cref=\"ShowMetroDialogAsync\"/> hasn't been called before.\n        /// </exception>\n        public static async Task HideMetroDialogAsync(this MetroWindow window, BaseMetroDialog dialog, MetroDialogSettings? settings = null)\n        {\n            window.Dispatcher.VerifyAccess();\n\n            if (window.metroActiveDialogContainer is null)\n            {\n                throw new InvalidOperationException(\"Active dialog container could not be found.\");\n            }\n\n            if (window.metroInactiveDialogContainer is null)\n            {\n                throw new InvalidOperationException(\"Inactive dialog container could not be found.\");\n            }\n\n            if (!window.metroActiveDialogContainer.Children.Contains(dialog) && !window.metroInactiveDialogContainer.Children.Contains(dialog))\n            {\n                throw new InvalidOperationException(\"The provided dialog is not visible in the specified window.\");\n            }\n\n            // once a button as been clicked, begin removing the dialog.\n            dialog.FireOnClose();\n\n            DialogClosed?.Invoke(window, new DialogStateChangedEventArgs(dialog));\n\n            await dialog.WaitForCloseAsync();\n\n            window.SizeChanged -= dialog.SizeChangedHandler;\n            window.RemoveDialog(dialog);\n\n            settings ??= dialog.DialogSettings;\n            await HandleOverlayOnHideAsync(settings, window);\n        }\n\n        /// <summary>\n        /// Gets the current shown dialog in async way.","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Controls/Dialogs/DialogManager.cs#L346-L382","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Call HideMetroDialogAsync exactly once per successful ShowMetroDialogAsync, and track the shown state in a field.","For built-in dialogs that self-close (ShowMessageAsync), do not call HideMetroDialogAsync afterward — await the returned task instead.","Guard the hide call: only hide if the dialog is still considered open (track a boolean or check the controller/await result).","Ensure you show and hide on the same MetroWindow instance."],"exampleFix":"// before\nawait window.ShowMetroDialogAsync(dialog);\n// ... user closes via dialog button which hides it ...\nawait window.HideMetroDialogAsync(dialog); // throws - already hidden\n\n// after\nawait window.ShowMetroDialogAsync(dialog);\nawait dialog.WaitUntilUnloadedAsync();\n// do not hide again; it is already removed","handlingStrategy":"validation","validationCode":"// Track whether the dialog is currently shown by this window before hiding.\nprivate bool _isShown;\n\nasync Task ShowHideAsync(MetroWindow window, BaseMetroDialog dialog)\n{\n    if (_isShown) return;\n    _isShown = true;\n    await window.ShowMetroDialogAsync(dialog);\n    await window.HideMetroDialogAsync(dialog);\n    _isShown = false;\n}\n\n// For built-in self-closing dialogs, await the task and do NOT call HideMetroDialogAsync.","typeGuard":null,"tryCatchPattern":"try { await window.HideMetroDialogAsync(dialog); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not visible\"))\n{\n    // Already hidden (e.g. self-closed). Safe to ignore.\n}","preventionTips":["Call HideMetroDialogAsync exactly once per shown dialog and track the state.","Do not call HideMetroDialogAsync on dialogs that self-close (ShowMessageAsync).","Show and hide on the same MetroWindow instance."],"tags":["wpf","dialogs","mahapps","state-management","lifecycle"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}