{"record":{"id":"8857cd97a7e504d9","repo":"PrismLibrary/Prism","slug":"unable-to-determine-the-current-page","errorCode":null,"errorMessage":"Unable to determine the current page.","messagePattern":"Unable to determine the current page\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Common/MvvmHelpers.cs","lineNumber":326,"sourceCode":"        };\n    }\n\n    private static Page? EvaluateCurrentPage(Page? target)\n    {\n        Page? child = GetTarget(target);\n\n        if (child is not null)\n            target = GetOnNavigatedToTargetFromChild(child);\n\n        if (target is { } page)\n        {\n            if (target is IDialogContainer)\n            {\n                return page.Parent switch\n                {\n                    Page parentPage => GetTarget(parentPage),\n                    Window window => GetTarget(window.Page),\n                    _ => throw new InvalidOperationException(\"Unable to determine the current page.\")\n                };\n            }\n\n            return page.Parent switch\n            {\n                TabbedPage tab when tab.CurrentPage != target => EvaluateCurrentPage(tab.CurrentPage),\n                NavigationPage nav when nav.CurrentPage != target => EvaluateCurrentPage(nav.CurrentPage),\n                _ => target\n            };\n        }\n\n        return null;\n    }\n\n    public static async Task HandleNavigationPageGoBack(NavigationPage navigationPage)\n    {\n        var navigationService = Navigation.Xaml.Navigation.GetNavigationService(navigationPage.CurrentPage);\n        var result = await navigationService.GoBackAsync();","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Common/MvvmHelpers.cs#L308-L344","documentation":"EvaluateCurrentPage walks the visual tree upward from a page (including dialog containers) to find the page Prism should treat as current. When a dialog container's parent is neither a Page nor a Window (or the walk otherwise cannot resolve a parent), Prism throws InvalidOperationException because the current page is undeterminable at that moment.","triggerScenarios":"Resolving the current page while an IDialogContainer page has a Parent that is neither Page nor Window; calling during startup/teardown before the page has been attached to a window; navigating pages whose parent chain does not end at a Window.","commonSituations":"Querying ViewModel location/navigation while a dialog is up and the dialog view has not been parented yet; calling app-level services that need the current page from a background thread or before MainActivity created the window; custom dialog hosting that breaks the parent chain.","solutions":["Ensure the current page is fully attached to the window hierarchy before asking for the current page (e.g. wait for OnNavigatedTo / window created).","Do not call current-page-dependent APIs while a dialog container is unparented; close the dialog first or hold an explicit page reference.","Verify you are using Prism's dialog service/window management rather than manually parenting dialog views.","Catch InvalidOperationException around current-page resolution and degrade gracefully."],"exampleFix":"// before\nvar vm = MvvmHelpers.GetCurrentPage(app.MainPage).BindingContext;\n// after\nif (Windows.Count > 0 && MvvmHelpers.GetCurrentPage(Windows[0].Page) is Page p)\n    var vm = p.BindingContext;","handlingStrategy":"fallback","validationCode":"var page = Application.Current?.Windows?.FirstOrDefault()?.Page;\nif (page is null) return; // not attached yet — skip current-page logic","typeGuard":"bool HasAttachedWindow(Page page) =>\n    page.Parent is Page or Window or null; // walk must terminate at a Window","tryCatchPattern":"try { var current = GetCurrentPage(); }\ncatch (InvalidOperationException) { current = Application.Current?.Windows?.FirstOrDefault()?.Page; }","preventionTips":["Only query the current page after the window/page is created (OnAppearing or later).","Avoid current-page lookups while a dialog container is unparented.","Keep an explicit reference to the active page in app state instead of walking the tree."],"tags":["prism","maui","dialogs","navigation"],"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"}