{"record":{"id":"009bcc14ada0eb72","repo":"dotnet/maui","slug":"either-set-nameof-mainpage-or-override-nameof","errorCode":null,"errorMessage":"Either set {nameof(MainPage)} or override {nameof(Application.CreateWindow)}.","messagePattern":"Either set (.+?) or override (.+?)\\.","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"critical","filePath":"src/Controls/src/Core/Application/Application.cs","lineNumber":579,"sourceCode":"\t\t{\n\t\t\tHandler?.Invoke(nameof(IApplication.ActivateWindow), window);\n\t\t}\n\n\t\tvoid IApplication.ThemeChanged()\n\t\t{\n\t\t\tPlatformAppTheme = AppInfo.RequestedTheme;\n\t\t}\n\n\t\tprotected virtual Window CreateWindow(IActivationState? activationState)\n\t\t{\n\t\t\tvar windowCreator = activationState?.Context.Services.GetService<IWindowCreator>();\n\t\t\tvar window = windowCreator?.CreateWindow(this, activationState);\n\t\t\tif (window is not null)\n\t\t\t\treturn window;\n\n\t\t\tif (Windows.Count > 1)\n#pragma warning disable CS0618 // Type or member is obsolete\n\t\t\t\tthrow new NotImplementedException($\"Either set {nameof(MainPage)} or override {nameof(Application.CreateWindow)}.\");\n#pragma warning restore CS0618 // Type or member is obsolete\n\n\t\t\tif (Windows.Count > 0)\n\t\t\t\treturn Windows[0];\n\n\t\t\tif (_singleWindowMainPage is not null)\n\t\t\t\treturn new Window(_singleWindowMainPage);\n\n#pragma warning disable CS0618 // Type or member is obsolete\n\t\t\tthrow new NotImplementedException($\"Either set {nameof(MainPage)} or override {nameof(Application.CreateWindow)}.\");\n#pragma warning restore CS0618 // Type or member is obsolete\n\t\t}\n\n\t\tinternal void AddWindow(Window window)\n\t\t{\n\t\t\t_windows.Add(window);\n\n\t\t\tif (window is Element windowElement)","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Application/Application.cs#L561-L597","documentation":"Thrown by the default Application.CreateWindow when it cannot produce a Window: no IWindowCreator service is registered, more than one Window already exists (ambiguous), and neither MainPage nor a CreateWindow override supplies a page. The default implementation can only fall back to MainPage, so without it the app cannot create its first window.","triggerScenarios":"CreateWindow is reached with a null windowCreator (no IWindowCreator service), Windows.Count > 1, and no override, so the first NotImplementedException 'Either set MainPage or override CreateWindow' fires.","commonSituations":"Fresh MAUI app where the developer removed the MainPage assignment but has not yet overridden CreateWindow; misconfigured host builder that did not register IWindowCreator; multi-window experiment left Windows.Count > 1 unexpectedly.","solutions":["Set Application.MainPage in the App constructor (e.g. MainPage = new AppShell()).","Override protected virtual Window CreateWindow(IActivationState) in your Application subclass and return a Window built from your page/shell.","Register an IWindowCreator via the host builder (builder.UseMauiApp...) if you want framework-driven window creation."],"exampleFix":"// before\npublic App() { /* nothing set */ }\n// after\npublic App() { MainPage = new AppShell(); }","handlingStrategy":"validation","validationCode":"// In your App, guarantee a Window source exists.\npublic App()\n{\n    if (Windows.Count == 0)\n        MainPage = new AppShell(); // or override CreateWindow\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set MainPage or override CreateWindow in your Application subclass.","Register IWindowCreator via the MAUI host if you want framework-driven window creation.","Add a startup smoke test that creates a Window and asserts it is non-null."],"tags":["application","window","startup","not-implemented"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}