{"record":{"id":"7e1dc9e0083f0183","repo":"dotnet/maui","slug":"unable-to-get-window-from-parent-view-controller-7e1dc9","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/iOS/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/iOS/MainViewController.cs#L32-L68","documentation":"This sample MainViewController (iOS) embeds a MAUI view into a UIKit layout. It resolves the UIWindow via `ParentViewController?.View?.Window`. If the controller is not yet in the view hierarchy, the window is null and embedding fails because the scenario needs a concrete UIWindow to create the MAUI root view.","triggerScenarios":"Executing this code in ViewDidLoad before the controller is attached to a window-backed parent. Calling the embed path during controller initialization or from a context where no parent controller exists.","commonSituations":"Lifecycle timing where ViewDidLoad runs before the view is in the window hierarchy. Presenting the controller without adding it to a parent that has a window. Testing in a unit/integration harness without a full UIWindow setup.","solutions":["Defer embedding to ViewDidAppear where Window is guaranteed available.","Ensure the controller is presented within a UINavigationController or UITabBarController that is the rootViewController of a UIWindow.","Add a guard that returns early if the window is null, with optional deferred retry."],"exampleFix":"// before (ViewDidLoad)\nvar window = ParentViewController?.View?.Window;\nif (window is null) throw new InvalidOperationException(...);\n\n// after (ViewDidAppear)\npublic override void ViewDidAppear(bool animated)\n{\n    base.ViewDidAppear(animated);\n    var window = View.Window;\n    if (window is null) return;\n    (_mauiView, _nativeView) = _scenario.Embed(window);\n}","handlingStrategy":"validation","validationCode":"var window = ParentViewController?.View?.Window;\nif (window is null)\n    return; // defer to ViewDidAppear\n(_mauiView, _nativeView) = _scenario.Embed(window);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Defer embedding to ViewDidAppear where the view hierarchy is fully attached.","Present the controller within a UINavigationController/UITabBarController rooted in a UIWindow.","Guard against null window instead of throwing."],"tags":["maui","ios","embedding","uikit","uiwindow","lifecycle"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}