dotnet/maui · error · InvalidOperationException
Both MainPage was set and CreateWindow was overridden to pro
Error message
Both MainPage was set and CreateWindow was overridden to provide a page.
What it means
Thrown during window creation when both Application.MainPage has been set AND the overridden CreateWindow returned a Window that already has a Page (window.Page != _singleWindowMainPage). MAUI cannot decide which page is authoritative, so it refuses rather than silently picking one.
Source
Thrown at src/Controls/src/Core/Application/Application.cs:486
if (activationState?.State?.TryGetValue(MauiWindowIdKey, out var requestedWindowId) ?? false)
{
if (requestedWindowId != null && _requestedWindows.TryGetValue(requestedWindowId, out var r))
{
if (r.TryGetTarget(out var w))
{
window = w;
}
_requestedWindows.Remove(requestedWindowId);
}
}
// create a new one if there is no pending windows
if (window == null)
{
window = CreateWindow(activationState);
if (_singleWindowMainPage != null && window.Page != null && window.Page != _singleWindowMainPage)
throw new InvalidOperationException($"Both {nameof(MainPage)} was set and {nameof(Application.CreateWindow)} was overridden to provide a page.");
}
// make sure it is added to the windows list
if (!_windows.Contains(window))
AddWindow(window);
return window;
}
void IApplication.OpenWindow(IWindow window)
{
if (window is Window cwindow)
{
OpenWindow(cwindow);
}
}
void IApplication.CloseWindow(IWindow window)View on GitHub (pinned to f377ff1c5e)
Solutions
- Choose one source of truth: either set MainPage (single-window convenience) or override CreateWindow to provide the page, not both.
- If you override CreateWindow, stop setting MainPage so _singleWindowMainPage stays null.
- If you keep MainPage, remove the Page assignment from your CreateWindow override so window.Page is null.
Example fix
// before
public App() { MainPage = new Page1(); }
protected override Window CreateWindow(IActivationState s) => new Window(new Page2());
// after
public App() { /* do not set MainPage */ }
protected override Window CreateWindow(IActivationState s) => new Window(new Page2()); Defensive patterns
Strategy: validation
Validate before calling
// Pick one source of truth for the first page.
static void ConfigureFirstPage(Application app, Func<Window> createWindow)
{
if (app.MainPage is not null)
throw new InvalidOperationException("Do not override CreateWindow to return a Page when MainPage is set.");
} Prevention
- Decide up front: convenience (MainPage) or explicit windows (override CreateWindow).
- Remove MainPage assignment the moment you override CreateWindow to return a Window with a Page.
- During migration, search the App constructor and CreateWindow together so only one path is active.
When it happens
Trigger: In the window-creation path, _singleWindowMainPage is non-null AND the Window returned by CreateWindow(activationState) has a non-null Page that differs from the single-window main page, triggering the InvalidOperationException.
Common situations: Migrating an app that sets MainPage in the constructor AND overrides CreateWindow to build its own Window/Page; half-finished multi-window migration leaving both code paths active; setting MainPage in OnStart while also overriding CreateWindow.
Related errors
- Either set {nameof(MainPage)} or override {nameof(Applicatio
- Please provide a main window in your app
- Unable to find Application Services
- Unable to find Application Services
- call Forms.Init() before this
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/1dcd6926b45e1474.
Report an issue: GitHub.