dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CanOnlyHaveOneChild, typeof(PageContent)…

Error message

SR.Format(SR.CanOnlyHaveOneChild, typeof(PageContent), value)

What it means

PageContent.AddChild throws InvalidOperationException with SR.CanOnlyHaveOneChild when a second child is added while _child is already set. A PageContent models exactly one XPS page, so it permits only a single FixedPage child.

Solutions

  1. Create a new PageContent for each page/child
  2. Check the existing Child before adding and reuse/replace it appropriately
  3. Remove or clear the existing child before calling AddChild again

Example fix

// before
pageContent.AddChild(page1);
pageContent.AddChild(page2);
// after
pageContent.AddChild(page1);
var pc2 = new PageContent();
pc2.AddChild(page2);
Defensive patterns

Strategy: validation

Validate before calling

if (pageContent.Child != null) { /* reuse or create new PageContent */ }

Type guard

bool CanAddChild(PageContent pc) => pc.Child == null;

Try / catch

try { pageContent.AddChild(child); } catch (InvalidOperationException) { pageContent = new PageContent(); pageContent.AddChild(child); }

Prevention

When it happens

Trigger: Calling AddChild twice on the same PageContent, or AddChild after the Child property was already assigned.

Common situations: Looping over a list of pages and adding each to the same PageContent instead of creating one PageContent per page; duplicated XAML content inside a PageContent element.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c9dc877e27c06f75. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/PageContent.cs:176

        /// Object to add as a child
        ///</param>
        /// <ExternalAPI/>
        void IAddChild.AddChild(Object value)
        {
//             VerifyAccess();

            ArgumentNullException.ThrowIfNull(value);


            FixedPage fp = value as FixedPage;
            if (fp == null)
            {
                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FixedPage)), nameof(value));
            }

            if (_child != null)
            {
                throw new InvalidOperationException(SR.Format(SR.CanOnlyHaveOneChild, typeof(PageContent), value));
            }

            _pageRef = null;
            _child   = fp;
            LogicalTreeHelper.AddLogicalChild(this, _child);
        }

        ///<summary>
        /// Called when text appears under the tag in markup
        ///</summary>
        ///<param name="text">
        /// Text to Add to the Object
        ///</param>
        /// <ExternalAPI/>
        void IAddChild.AddText(string text)
        {
            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
        }

View on GitHub (pinned to 81131a70a4)