dotnet/wpf · error · InvalidOperationException
SR.NoAddChild
Error message
SR.NoAddChild
What it means
NavigationWindow overrides AddChild to explicitly forbid adding object children through the XAML content-model, since its content must be set via the Content property / navigation. Any attempt to append a child (e.g. XAML '<NavigationWindow><Button/>') throws.
Solutions
- Set NavigationWindow.Content to a single element instead of adding children.
- Put your UI in a Page and navigate to it (Source or Navigate()).
- Wrap multiple elements in a container (Grid/StackPanel) assigned to Content.
- Remove child elements from NavigationWindow XAML and host them in the navigated page.
Example fix
// before
<NavigationWindow ...>
<Button Content="OK"/>
</NavigationWindow>
// after
<NavigationWindow x:Name="navWin" Source="MainPage.xaml" ... />
// or navWin.Content = new Button{ Content="OK" }; Defensive patterns
Strategy: type-guard
Validate before calling
if (navWindow is NavigationWindow) { navWindow.Content = element; } else { navWindow.AddChild(element); } Type guard
void SetContent(Window w, object c) { if (w is NavigationWindow) ((NavigationWindow)w).Content = c; else w.Content = c; } Try / catch
try { navWindow.Content = element; }
catch (InvalidOperationException) { /* fall back to Page navigation */ } Prevention
- Never nest elements directly under <NavigationWindow> in XAML.
- Always use Content or Navigate()/Source for NavigationWindow UI.
- Replace AddChild/AddText calls when migrating from Window to NavigationWindow.
- Wrap multi-element UIs in a single panel assigned to Content.
When it happens
Trigger: Calling navigationWindow.AddChild(obj) directly, or declaring child elements inside a NavigationWindow in XAML, which routes through AddChild during parsing.
Common situations: Migrating from Window to NavigationWindow while keeping child-element XAML; programmatic UI construction code calling AddChild on a NavigationWindow.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Animation_NoTextChildren
- SR.ParserCanOnlyHaveOneChild
- Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
- Animation_NoTextChildren
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3a08890a1f38e188.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Navigation/NavigationWindow.cs:838
//
// Protected Methods
//
//------------------------------------------------------
#region Protected Methods
/// <summary>
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
/// </summary>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new NavigationWindowAutomationPeer(this);
}
/// <summary>
/// Add an object child to this control
/// </summary>
protected override void AddChild(object value)
{
throw new InvalidOperationException(SR.NoAddChild);
}
/// <summary>
/// Add a text string to this control
/// </summary>
protected override void AddText(string text)
{
XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
}
/// <summary>
/// This even fires when window is closed. This event is non cancelable and is
/// for user infromational purposes
/// </summary>
/// <remarks>
/// This method follows the .Net programming guideline of having a protected virtual
/// method that raises an event, to provide a convenience for developers that subclass
/// the event. If you override this method - you need to call Base.OnClosed(...) forView on GitHub (pinned to 81131a70a4)