{"record":{"id":"1dcd6926b45e1474","repo":"dotnet/maui","slug":"both-mainpage-was-set-and-createwindow-was-overrid","errorCode":null,"errorMessage":"Both MainPage was set and CreateWindow was overridden to provide a page.","messagePattern":"Both MainPage was set and CreateWindow was overridden to provide a page\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Application/Application.cs","lineNumber":486,"sourceCode":"\t\t\tif (activationState?.State?.TryGetValue(MauiWindowIdKey, out var requestedWindowId) ?? false)\n\t\t\t{\n\t\t\t\tif (requestedWindowId != null && _requestedWindows.TryGetValue(requestedWindowId, out var r))\n\t\t\t\t{\n\t\t\t\t\tif (r.TryGetTarget(out var w))\n\t\t\t\t\t{\n\t\t\t\t\t\twindow = w;\n\t\t\t\t\t}\n\t\t\t\t\t_requestedWindows.Remove(requestedWindowId);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// create a new one if there is no pending windows\n\t\t\tif (window == null)\n\t\t\t{\n\t\t\t\twindow = CreateWindow(activationState);\n\n\t\t\t\tif (_singleWindowMainPage != null && window.Page != null && window.Page != _singleWindowMainPage)\n\t\t\t\t\tthrow new InvalidOperationException($\"Both {nameof(MainPage)} was set and {nameof(Application.CreateWindow)} was overridden to provide a page.\");\n\t\t\t}\n\n\t\t\t// make sure it is added to the windows list\n\t\t\tif (!_windows.Contains(window))\n\t\t\t\tAddWindow(window);\n\n\t\t\treturn window;\n\t\t}\n\n\t\tvoid IApplication.OpenWindow(IWindow window)\n\t\t{\n\t\t\tif (window is Window cwindow)\n\t\t\t{\n\t\t\t\tOpenWindow(cwindow);\n\t\t\t}\n\t\t}\n\n\t\tvoid IApplication.CloseWindow(IWindow window)","sourceCodeStart":468,"sourceCodeEnd":504,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Application/Application.cs#L468-L504","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic App() { MainPage = new Page1(); }\nprotected override Window CreateWindow(IActivationState s) => new Window(new Page2());\n// after\npublic App() { /* do not set MainPage */ }\nprotected override Window CreateWindow(IActivationState s) => new Window(new Page2());","handlingStrategy":"validation","validationCode":"// Pick one source of truth for the first page.\nstatic void ConfigureFirstPage(Application app, Func<Window> createWindow)\n{\n    if (app.MainPage is not null)\n        throw new InvalidOperationException(\"Do not override CreateWindow to return a Page when MainPage is set.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["application","window","startup","multi-window"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}