{"record":{"id":"c9dc877e27c06f75","repo":"dotnet/wpf","slug":"sr-format-sr-canonlyhaveonechild-typeof-pagecontent-value","errorCode":null,"errorMessage":"SR.Format(SR.CanOnlyHaveOneChild, typeof(PageContent), value)","messagePattern":"SR\\.Format\\(SR\\.CanOnlyHaveOneChild, typeof\\(PageContent\\), value\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/PageContent.cs","lineNumber":176,"sourceCode":"        /// Object to add as a child\n        ///</param>\n        /// <ExternalAPI/>\n        void IAddChild.AddChild(Object value)\n        {\n//             VerifyAccess();\n\n            ArgumentNullException.ThrowIfNull(value);\n\n\n            FixedPage fp = value as FixedPage;\n            if (fp == null)\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(FixedPage)), nameof(value));\n            }\n\n            if (_child != null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CanOnlyHaveOneChild, typeof(PageContent), value));\n            }\n\n            _pageRef = null;\n            _child   = fp;\n            LogicalTreeHelper.AddLogicalChild(this, _child);\n        }\n\n        ///<summary>\n        /// Called when text appears under the tag in markup\n        ///</summary>\n        ///<param name=\"text\">\n        /// Text to Add to the Object\n        ///</param>\n        /// <ExternalAPI/>\n        void IAddChild.AddText(string text)\n        {\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/PageContent.cs#L158-L194","documentation":"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.","triggerScenarios":"Calling AddChild twice on the same PageContent, or AddChild after the Child property was already assigned.","commonSituations":"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.","solutions":["Create a new PageContent for each page/child","Check the existing Child before adding and reuse/replace it appropriately","Remove or clear the existing child before calling AddChild again"],"exampleFix":"// before\npageContent.AddChild(page1);\npageContent.AddChild(page2);\n// after\npageContent.AddChild(page1);\nvar pc2 = new PageContent();\npc2.AddChild(page2);","handlingStrategy":"validation","validationCode":"if (pageContent.Child != null) { /* reuse or create new PageContent */ }","typeGuard":"bool CanAddChild(PageContent pc) => pc.Child == null;","tryCatchPattern":"try { pageContent.AddChild(child); } catch (InvalidOperationException) { pageContent = new PageContent(); pageContent.AddChild(child); }","preventionTips":["One PageContent per page in generation loops","Check Child/AddChild state before mutation","Build FixedDocument by appending new PageContent per page"],"tags":["wpf","invalid-operation","xps","one-child"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}