{"record":{"id":"e5761cf22b446b46","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"no-loaded-dialoghost-instances","errorCode":null,"errorMessage":"No loaded DialogHost instances.","messagePattern":"No loaded DialogHost instances\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/DialogHost.cs","lineNumber":222,"sourceCode":"    /// <param name=\"dialogIdentifier\">The identifier to use to retrieve the DialogHost</param>\n    /// <returns>The DialogSession if one is in process, or null</returns>\n    public static DialogSession? GetDialogSession(object? dialogIdentifier)\n    {\n        DialogHost dialogHost = GetInstance(dialogIdentifier);\n        return dialogHost.CurrentSession;\n    }\n\n    /// <summary>\n    /// dialog instance exists\n    /// </summary>\n    /// <param name=\"dialogIdentifier\">of the instance where the dialog should be closed. Typically this will match an identifier set in XAML.</param>\n    /// <returns></returns>\n    public static bool IsDialogOpen(object? dialogIdentifier) => GetDialogSession(dialogIdentifier)?.IsEnded == false;\n\n    private static DialogHost GetInstance(object? dialogIdentifier)\n    {\n        if (LoadedInstances.Count == 0)\n            throw new InvalidOperationException(\"No loaded DialogHost instances.\");\n\n        List<DialogHost> targets = [];\n        foreach (var instance in LoadedInstances.ToList())\n        {\n            if (instance.TryGetTarget(out DialogHost? dialogInstance))\n            {\n                object? identifier = null;\n                if (dialogInstance.CheckAccess())\n                {\n                    identifier = dialogInstance.Identifier;\n                }\n                else\n                {\n                    // Retrieve the identifier using the Dispatcher on the owning thread (effectively replaces the VerifyAccess() call previously used)\n                    identifier = dialogInstance.Dispatcher.Invoke(() => dialogInstance.Identifier);\n                }\n                if (Equals(dialogIdentifier, identifier))\n                {","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/DialogHost.cs#L204-L240","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure at least one `<materialDesign:DialogHost Identifier=\"...\" />` is present in a live visual tree (typically near the window root) before calling Show/Close.","Defer dialog calls until the host's `Loaded` event (or the window's `OnContentRendered`).","Keep the host alive for the lifetime you intend to show dialogs (e.g. on the main window, not a transient view).","Verify with `DialogHost.IsDialogOpen`/a custom check that a host instance is loaded."],"exampleFix":"<!-- before: no DialogHost in the window -> Show throws 'No loaded DialogHost instances.' -->\n<Window> ... <!-- no DialogHost --> </Window>\n\n<!-- after -->\n<Window>\n  <materialDesign:DialogHost Identifier=\"RootDialog\" />\n</Window>","handlingStrategy":"validation","validationCode":"// Ensure a DialogHost is loaded before any Show/Close:\n// place <materialDesign:DialogHost Identifier=\"...\"/> in the window,\n// then defer calls until Loaded:\nhost.Loaded += async (s, e) => await DialogHost.Show(content, id);","typeGuard":null,"tryCatchPattern":"try { await DialogHost.Show(content, id); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"No loaded DialogHost\"))\n{ /* show inline fallback / log: no host mounted */ }","preventionTips":["Mount a DialogHost in a long-lived window before calling Show/Close.","Defer dialog calls until the host's Loaded event.","Don't open dialogs from startup services before the UI is up."],"tags":["wpf","dialog","lifecycle","visual-tree","material-design"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}