{"record":{"id":"6878f12d440aa77e","repo":"unoplatform/uno","slug":"the-window-should-be-created-later-in-the-applicat","errorCode":null,"errorMessage":"The Window should be created later in the application lifecycle.","messagePattern":"The Window should be created later in the application lifecycle\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/SamplesApp/SamplesApp.Shared/App.xaml.cs","lineNumber":361,"sourceCode":"\t\tprivate void ActivateMainWindow()\n\t\t{\n#if DEBUG && (__SKIA__ || __WASM__)\n\t\t\t_mainWindow!.EnableHotReload();\n#endif\n\t\t\t// await Task.Delay(15000); // Artificial delay to simulate asynchronous activation\n\t\t\t_mainWindow!.Activate();\n\t\t\t_wasActivated = true;\n\t\t\tMainWindowActivated?.Invoke(this, EventArgs.Empty);\n\t\t}\n\n\t\tpublic event EventHandler? MainWindowActivated;\n\n#if HAS_UNO && !HAS_UNO_WINUI\n\t\tprotected override void OnWindowCreated(global::Microsoft.UI.Xaml.WindowCreatedEventArgs args)\n\t\t{\n\t\t\tif (Current is null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"The Window should be created later in the application lifecycle.\");\n\t\t\t}\n\t\t}\n#endif\n\n\t\tprivate void InitializeFrame(string? arguments = null)\n\t\t{\n\t\t\tif (_mainWindow is null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"Main window must be initialized before Frame\");\n\t\t\t}\n\n\t\t\tvar rootFrame = _mainWindow.Content as Frame;\n\n\t\t\t// Do not repeat app initialization when the Window already has content,\n\t\t\t// just ensure that the window is active\n\t\t\tif (rootFrame is null)\n\t\t\t{\n\t\t\t\t// Create a Frame to act as the navigation context and navigate to the first page","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/SamplesApp/SamplesApp.Shared/App.xaml.cs#L343-L379","documentation":"Thrown in OnWindowCreate (under HAS_UNO && !HAS_UNO_WINUI) when the app's static Current is null at the moment a Window is created. Uno expects the application singleton to be initialized before any Window is constructed; a Window built too early in the lifecycle violates that ordering.","triggerScenarios":"Constructing a Microsoft.UI.Xaml.Window before the Application instance is fully initialized — e.g. creating a window in a static constructor, before OnLaunched, or during host build before Current is assigned.","commonSituations":"App startup reordering; creating a splash or secondary window during service registration; custom main() that instantiates Window before Application.Run; refactoring that moved window creation earlier.","solutions":["Defer all Window construction until after the Application is initialized (OnLaunched or later).","Ensure Current is set (the framework sets it during Application startup) before any new Window().","If you have a custom entry point, call the standard Uno host build that sets Current first.","Move window creation out of static constructors and field initializers."],"exampleFix":"// before - window built in a static/field initializer\npublic static Window Splash = new Window();\n// after - build it in OnLaunched\nprotected override void OnLaunched(LaunchActivatedEventArgs e)\n{\n    var splash = new Window();\n    splash.Activate();\n}","handlingStrategy":"validation","validationCode":"if (Application.Current is null) throw new InvalidOperationException(\"Initialize the Application before creating any Window.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never construct a Window in static/field initializers.","Create windows in OnLaunched or later, after Current is set.","Use the standard Uno host build to guarantee initialization order."],"tags":["app-lifecycle","window","startup","uno","ordering"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}