dotnet/maui · error · InvalidOperationException
Unable to get window from parent view controller
Error message
Unable to get window from parent view controller
What it means
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.
Source
Thrown at src/Controls/samples/Controls.Sample.Embedding/Platforms/iOS/MainViewController.cs:50
stackView.TrailingAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TrailingAnchor, -20),
stackView.BottomAnchor.ConstraintLessThanOrEqualTo(View.SafeAreaLayoutGuide.BottomAnchor, -20)
});
// Create UIKit button
var firstButton = new UIButton(UIButtonType.System);
firstButton.SetTitle("UIKit Button Above MAUI", UIControlState.Normal);
stackView.AddArrangedSubview(firstButton);
// Uncomment the scenario to test:
// _scenario = new EmbeddingScenarios.Scenario1_Basic();
//_scenario = new EmbeddingScenarios.Scenario2_Scoped();
_scenario = new EmbeddingScenarios.Scenario3_Correct();
// create the view and (maybe) the window
var window = ParentViewController?.View?.Window;
if (window is null)
{
throw new InvalidOperationException("Unable to get window from parent view controller");
}
(_mauiView, _nativeView) = _scenario.Embed(window);
// add the new view to the UI
stackView.AddArrangedSubview(new ContainerView(_nativeView));
// Create UIKit button
var secondButton = new UIButton(UIButtonType.System);
secondButton.SetTitle("UIKit Button Below MAUI", UIControlState.Normal);
stackView.AddArrangedSubview(secondButton);
// Create UIKit button
var thirdButton = new UIButton(UIButtonType.System);
thirdButton.SetTitle("UIKit Button Magic", UIControlState.Normal);
thirdButton.TouchUpInside += OnMagicClicked;
stackView.AddArrangedSubview(thirdButton);
View on GitHub (pinned to f377ff1c5e)
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.
Example fix
// before (ViewDidLoad)
var window = ParentViewController?.View?.Window;
if (window is null) throw new InvalidOperationException(...);
// after (ViewDidAppear)
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
var window = View.Window;
if (window is null) return;
(_mauiView, _nativeView) = _scenario.Embed(window);
} Defensive patterns
Strategy: validation
Validate before calling
var window = ParentViewController?.View?.Window;
if (window is null)
return; // defer to ViewDidAppear
(_mauiView, _nativeView) = _scenario.Embed(window); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to get window from parent view controller
- You MUST invoke LoadApplication () before calling base.Finis
- Unable to load system image 'macwindow.badge.plus'
- No UIViewController found to present.
- Reuse of the Shell Renderer is not supported
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/7e1dc9e0083f0183.
Report an issue: GitHub.