PrismLibrary/Prism · error · InvalidOperationException

Unable to determine the current page.

Error message

Unable to determine the current page.

What it means

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.

Solutions

  1. Ensure the current page is fully attached to the window hierarchy before asking for the current page (e.g. wait for OnNavigatedTo / window created).
  2. Do not call current-page-dependent APIs while a dialog container is unparented; close the dialog first or hold an explicit page reference.
  3. Verify you are using Prism's dialog service/window management rather than manually parenting dialog views.
  4. Catch InvalidOperationException around current-page resolution and degrade gracefully.

Example fix

// before
var vm = MvvmHelpers.GetCurrentPage(app.MainPage).BindingContext;
// after
if (Windows.Count > 0 && MvvmHelpers.GetCurrentPage(Windows[0].Page) is Page p)
    var vm = p.BindingContext;
Defensive patterns

Strategy: fallback

Validate before calling

var page = Application.Current?.Windows?.FirstOrDefault()?.Page;
if (page is null) return; // not attached yet — skip current-page logic

Type guard

bool HasAttachedWindow(Page page) =>
    page.Parent is Page or Window or null; // walk must terminate at a Window

Try / catch

try { var current = GetCurrentPage(); }
catch (InvalidOperationException) { current = Application.Current?.Windows?.FirstOrDefault()?.Page; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/8857cd97a7e504d9. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Common/MvvmHelpers.cs:326

        };
    }

    private static Page? EvaluateCurrentPage(Page? target)
    {
        Page? child = GetTarget(target);

        if (child is not null)
            target = GetOnNavigatedToTargetFromChild(child);

        if (target is { } page)
        {
            if (target is IDialogContainer)
            {
                return page.Parent switch
                {
                    Page parentPage => GetTarget(parentPage),
                    Window window => GetTarget(window.Page),
                    _ => throw new InvalidOperationException("Unable to determine the current page.")
                };
            }

            return page.Parent switch
            {
                TabbedPage tab when tab.CurrentPage != target => EvaluateCurrentPage(tab.CurrentPage),
                NavigationPage nav when nav.CurrentPage != target => EvaluateCurrentPage(nav.CurrentPage),
                _ => target
            };
        }

        return null;
    }

    public static async Task HandleNavigationPageGoBack(NavigationPage navigationPage)
    {
        var navigationService = Navigation.Xaml.Navigation.GetNavigationService(navigationPage.CurrentPage);
        var result = await navigationService.GoBackAsync();

View on GitHub (pinned to 358118cd64)