{"record":{"id":"eba7691cfcec4d87","repo":"PrismLibrary/Prism","slug":"there-is-no-prism-window-currently-displayed","errorCode":null,"errorMessage":"There is no Prism Window currently displayed.","messagePattern":"There is no Prism Window currently displayed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Services/PageDialogs/PageDialogService.cs","lineNumber":186,"sourceCode":"    /// <param name=\"title\">Title to display</param>\n    /// <param name=\"message\">Message to display</param>\n    /// <param name=\"accept\">Text for the accept button</param>\n    /// <param name=\"cancel\">Text for the cancel button</param>\n    /// <param name=\"placeholder\">Placeholder text to display in the prompt</param>\n    /// <param name=\"maxLength\">Maximum length of the user response</param>\n    /// <param name=\"keyboardType\">Keyboard type to use for the user response</param>\n    /// <param name=\"initialValue\">Pre-defined response that will be displayed, and which can be edited</param>\n    /// <returns><c>string</c> entered by the user. <c>null</c> if cancel is pressed</returns>\n    public virtual Task<string> DisplayPromptAsync(string title, string message, string accept = \"OK\", string cancel = \"Cancel\", string placeholder = default, int maxLength = -1, KeyboardType keyboardType = KeyboardType.Default, string initialValue = \"\")\n    {\n        var keyboard = _keyboardMapper.Map(keyboardType);\n        return GetPage().DisplayPromptAsync(title, message, accept, cancel, placeholder, maxLength, keyboard, initialValue);\n    }\n\n    private Page GetPage()\n    {\n        if (_windowManager.Current is not PrismWindow window)\n            throw new InvalidOperationException(\"There is no Prism Window currently displayed.\");\n\n        return window.CurrentPage;\n    }\n}\n","sourceCodeStart":168,"sourceCodeEnd":191,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Services/PageDialogs/PageDialogService.cs#L168-L191","documentation":"PageDialogService.GetPage resolves the current window via IWindowManager and requires it to be a PrismWindow; otherwise it throws this InvalidOperationException. Dialog APIs (alerts, action sheets, prompts) need a live Prism window and current page to display on.","triggerScenarios":"Calling DisplayAlertAsync/DisplayActionSheetAsync/DisplayPromptAsync when no PrismWindow is currently displayed — e.g. before the app window is created, after the window was closed, when using a custom window not derived from PrismWindow, or from a background context (ViewModel constructed pre-window, service, or unit test).","commonSituations":"Showing dialogs during startup before CreateWindow completes; attempting dialogs after the last window closed on desktop platforms; replacing PrismWindow with a custom Window type; calling dialog service from a headless service/test.","solutions":["Only invoke dialog APIs after the Prism window is shown — e.g. defer dialog calls until after initial navigation completes (OnNavigatedTo or an event after window activation).","Ensure the app window is a PrismWindow (created through Prism's CreateWindow pipeline, not a raw Microsoft.Maui.Controls.Window).","Check IWindowManager.Current for null/non-PrismWindow before calling dialog methods.","In tests or services, abstract the dialog service behind an interface and inject a fake instead of hitting the real window."],"exampleFix":"// before: dialog during constructor, before window exists\npublic MainViewModel(IPageDialogService dialogs) => dialogs.DisplayAlertAsync(\"Hi\", \"Welcome\", \"OK\");\n// after: show after navigation\npublic void OnNavigatedTo(INavigationParameters parameters) =>\n    _dialogs.DisplayAlertAsync(\"Hi\", \"Welcome\", \"OK\");","handlingStrategy":"validation","validationCode":"bool canShowDialog = _windowManager.Current is PrismWindow w && w.CurrentPage is not null;","typeGuard":"static bool HasActivePrismWindow(IWindowManager wm) => wm.Current is PrismWindow;","tryCatchPattern":"if (_windowManager.Current is not PrismWindow)\n    return; // or queue the dialog\nawait _dialogs.DisplayAlertAsync(title, message, \"OK\");","preventionTips":["Show dialogs only after navigation/window activation (e.g. OnNavigatedTo), never in constructors.","Do not replace PrismWindow with a custom Window type when using PageDialogService.","Queue or drop dialogs requested while no window is displayed.","Use a fake IPageDialogService in tests and headless services."],"tags":["dotnet","maui","prism","dialogs","window"],"backgroundTag":"invalid-state-transition","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}