{"record":{"id":"108fb7db26fc46ec","repo":"dotnet/wpf","slug":"sr-format-sr-templatenotcollected-writevalue","errorCode":null,"errorMessage":"SR.Format(SR.TemplateNotCollected, \"WriteValue\")","messagePattern":"SR\\.Format\\(SR\\.TemplateNotCollected, \"WriteValue\"\\)","errorType":"exception","errorClass":"XamlInternalException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/DeferredWriter.cs","lineNumber":196,"sourceCode":"                _deferredWriter.WriteEndMember();\n                _handled = true;\n                break;\n\n            default:\n                throw new XamlInternalException(SR.Format(SR.MissingCase, _mode.ToString(), \"WriteEndMember\"));\n            }\n        }\n\n        public override void WriteValue(object value)\n        {\n            _handled = false;\n            switch (_mode)\n            {\n            case DeferringMode.Off:\n                break;\n\n            case DeferringMode.TemplateReady:\n                throw new XamlInternalException(SR.Format(SR.TemplateNotCollected, \"WriteValue\"));\n\n            case DeferringMode.TemplateStarting:\n                // This handles the case of SM template; V object; EM\n                Debug.Assert(_deferredTreeDepth == 0);\n                if (value is XamlNodeList)\n                {\n                    _deferredList = (XamlNodeList)value;\n                    _mode = DeferringMode.TemplateReady;\n                    _handled = true;\n                }\n                else\n                {\n                    StartDeferredList();\n                    _mode = DeferringMode.TemplateDeferring;\n                    goto case DeferringMode.TemplateDeferring;\n                }\n\n                break;","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/DeferredWriter.cs#L178-L214","documentation":"XamlInternalException thrown by XamlDeferringWriter.WriteValue when the writer is in TemplateReady deferring mode, meaning a deferred template's node list exists but has not been collected/loaded yet, so writing a value at that point would corrupt the node stream. System.Xaml throws this because template deferral requires StartObject/EndObject transitions before values can be accepted.","triggerScenarios":"Calling WriteValue on a XamlDeferringWriter (obtained via XamlObjectWriterSettings/XamlXmlWriter template deferral) while _mode == DeferringMode.TemplateReady — i.e. after a deferred template has been prepared but before its node collection phase has started or completed.","commonSituations":"Custom XAML writers/readers that hand-craft node streams around markup-extension templates; feeding nodes out of order to a deferring writer; mixing a XamlNodeList-producing pipeline with direct Write* calls on the same writer.","solutions":["Fix the node-stream ordering: ensure WriteStartObject/WriteEndObject (template collection) transitions occur before calling WriteValue in a deferred-template context.","If the intent is to write values into the deferred template, use the deferred node list (XamlNodeList.Writer) rather than the outer deferring writer.","Do not interleave direct Write* calls with deferred template collection on the same writer; complete one mode before switching.","Treat this as a bug in the calling writer implementation — XamlInternalException indicates a violated internal protocol, not user input error."],"exampleFix":"// before (wrong: WriteValue while template is ready but not collected)\nwriter.WriteStartObject(templateType);\nwriter.WriteValue(\"some value\"); // throws XamlInternalException\n// after\nwriter.WriteStartObject(templateType);\nwriter.WriteEndObject(); // complete the template collection transition first\nwriter.WriteValue(\"some value\");","handlingStrategy":"validation","validationCode":"if (deferringWriter.Mode == DeferringMode.TemplateReady)\n    throw new InvalidOperationException(\"Complete template collection transitions before WriteValue.\");","typeGuard":"static bool CanWriteValue(XamlDeferringWriter w) => w.Mode != DeferringMode.TemplateReady;","tryCatchPattern":"try { writer.WriteValue(v); }\ncatch (XamlInternalException ex) { LogProtocolBug(ex); throw; } // never swallow: indicates caller bug","preventionTips":["Never interleave direct Write* calls with deferred-template collection on one writer.","Route template-body nodes through the XamlNodeList writer, not the outer deferring writer.","Complete StartObject/EndObject transitions before values.","Treat XamlInternalException as a caller-protocol bug, not recoverable input."],"tags":["xaml","internal-exception","node-stream","template-deferral"],"backgroundTag":"internal-invariant-violation","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"}