{"record":{"id":"b3daa750ad585a2f","repo":"dotnet/wpf","slug":"sr-prevoiusuninitializeddocumentreferenceoutstanding","errorCode":null,"errorMessage":"SR.PrevoiusUninitializedDocumentReferenceOutstanding","messagePattern":"SR\\.PrevoiusUninitializedDocumentReferenceOutstanding","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs","lineNumber":118,"sourceCode":"            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(DocumentReference)), nameof(value));\n            }\n\n            if (docRef.IsInitialized)\n            {\n                _references.Add(docRef);\n            }\n            else\n            {\n                DocumentsTrace.FixedDocumentSequence.Content.Trace($\"Doc {_references.Count} Deferred\");\n                if (_partialRef == null)\n                {\n                    _partialRef = docRef;\n                    _partialRef.Initialized += new EventHandler(_OnDocumentReferenceInitialized);\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.PrevoiusUninitializedDocumentReferenceOutstanding);\n                }\n            }\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        void IAddChild.AddText(string text)\n        {\n            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);\n        }\n\n        #endregion\n\n        #region IFixedNavigate","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequence.cs#L100-L136","documentation":"DocumentSequence.AddChild tracks one partially-initialized DocumentReference (_partialRef) at a time, waiting for its Initialized event before accepting the next child. If a previous DocumentReference is still uninitialized when AddChild is called again, it throws InvalidOperationException with SR.PrevoiusUninitializedDocumentReferenceOutstanding.","triggerScenarios":"Adding a second DocumentReference while the first has not yet been initialized (its Source/Id not resolved and Initialized event not fired) — e.g. rapid programmatic AddChild calls without setting up each reference fully, or malformed markup that skips initialization.","commonSituations":"Programmatically adding many DocumentReferences in a loop without assigning each one's Document/Source; a reference whose Source fails to resolve leaving the pending state stuck; unusual parsing scenarios where initialization callbacks are deferred.","solutions":["Fully initialize each DocumentReference (set Document or a resolvable Source) before adding the next one","Wait for the previous DocumentReference's Initialized event before calling AddChild again","Use data binding / the standard XAML DocumentReference pattern instead of manual AddChild","Check that the earlier reference's Source is valid so initialization can complete"],"exampleFix":"// before\nforeach (var doc in docs) { seq.AddChild(doc); } // refs not initialized\n// after\nforeach (var doc in docs)\n{\n    if (!doc.IsInitialized) doc.Document = ResolveDocument(doc);\n    seq.AddChild(doc);\n}","handlingStrategy":"validation","validationCode":"if (pendingRef != null && !pendingRef.IsInitialized) throw new InvalidOperationException(\"Previous DocumentReference not initialized\");","typeGuard":null,"tryCatchPattern":"try { seq.AddChild(docRef); } catch (InvalidOperationException ex) { log.Error(\"Outstanding uninitialized DocumentReference\", ex); }","preventionTips":["Fully initialize (set Document or Source) each reference before adding the next","Subscribe to Initialized and add sequentially in the handler","Prefer standard XAML patterns over manual AddChild"],"tags":["wpf","invalid-state","ordering"],"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-22T01:17:13.364Z"}