{"record":{"id":"f3067f305be142ba","repo":"dotnet/maui","slug":"unable-to-get-window-from-parent-view-controller","errorCode":null,"errorMessage":"Unable to get window from parent view controller","messagePattern":"Unable to get window from parent view controller","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/samples/Controls.Sample.Embedding/Platforms/MacCatalyst/MainViewController.cs","lineNumber":50,"sourceCode":"\t\t\tstackView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor, -20),\n\t\t\tstackView.BottomAnchor.ConstraintLessThanOrEqualTo(View.SafeAreaLayoutGuide.BottomAnchor, -20)\n\t\t});\n\n\t\t// Create UIKit button\n\t\tvar firstButton = new UIButton(UIButtonType.System);\n\t\tfirstButton.SetTitle(\"UIKit Button Above MAUI\", UIControlState.Normal);\n\t\tstackView.AddArrangedSubview(firstButton);\n\n\t\t// Uncomment the scenario to test:\n\t\t//_scenario = new EmbeddingScenarios.Scenario1_Basic();\n\t\t//_scenario = new EmbeddingScenarios.Scenario2_Scoped();\n\t\t_scenario = new EmbeddingScenarios.Scenario3_Correct();\n\n\t\t// create the view and (maybe) the window\n\t\tvar window = ParentViewController?.View?.Window;\n\t\tif (window is null)\n\t\t{\n\t\t\tthrow new InvalidOperationException(\"Unable to get window from parent view controller\");\n\t\t}\n\n\t\t(_mauiView, _nativeView) = _scenario.Embed(window);\n\n\t\t// add the new view to the UI\n\t\tstackView.AddArrangedSubview(new ContainerView(_nativeView));\n\n\t\t// Create UIKit button\n\t\tvar secondButton = new UIButton(UIButtonType.System);\n\t\tsecondButton.SetTitle(\"UIKit Button Below MAUI\", UIControlState.Normal);\n\t\tstackView.AddArrangedSubview(secondButton);\n\n\t\t// Create UIKit button\n\t\tvar thirdButton = new UIButton(UIButtonType.System);\n\t\tthirdButton.SetTitle(\"UIKit Button Magic\", UIControlState.Normal);\n\t\tthirdButton.TouchUpInside += OnMagicClicked;\n\t\tstackView.AddArrangedSubview(thirdButton);\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/samples/Controls.Sample.Embedding/Platforms/MacCatalyst/MainViewController.cs#L32-L68","documentation":"This sample MainViewController (MacCatalyst) embeds a MAUI view inside a UIKit UI. It retrieves the parent UIWindow via `ParentViewController?.View?.Window`. If the controller is not yet in the view hierarchy (no parent, or the parent's view has no window), the window is null and embedding cannot proceed because the scenario requires a concrete UIWindow to host the MAUI root view.","triggerScenarios":"Accessing this code path during ViewDidLoad when the controller has not yet been added to the window hierarchy. Calling Embed before the view is added as a child of a window-backed controller. Using the controller outside of a normal presentation flow.","commonSituations":"Presenting the controller programmatically before it is attached to a window. Lifecycle timing issues where ViewDidLoad fires before the parent's View.Window is available. Testing the controller in isolation without a full window setup.","solutions":["Move the embedding logic to ViewDidAppear or viewWillAppear, where the view hierarchy (including Window) is guaranteed to be set up.","Ensure the controller is presented as a child of a window-backed parent controller before this code runs.","Guard with a retry or deferred execution if the window is not yet available."],"exampleFix":"// before (ViewDidLoad — window may be null)\nvar window = ParentViewController?.View?.Window;\nif (window is null) throw new InvalidOperationException(...);\n\n// after (ViewDidAppear — window is available)\npublic override void ViewDidAppear(bool animated)\n{\n    base.ViewDidAppear(animated);\n    var window = ParentViewController?.View?.Window;\n    if (window is null) return; // not ready yet\n    (_mauiView, _nativeView) = _scenario.Embed(window);\n}","handlingStrategy":"validation","validationCode":"// Defer embedding until the window is available\nvar window = ParentViewController?.View?.Window;\nif (window is null)\n    return; // will be available in ViewDidAppear\n(_mauiView, _nativeView) = _scenario.Embed(window);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Move embedding logic to ViewDidAppear or viewDidLayoutSubviews where Window is guaranteed.","Ensure the controller is presented as a child of a window-backed parent.","Guard against null window and defer rather than throwing."],"tags":["maui","maccatalyst","embedding","uikit","uiwindow","lifecycle"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}