{"record":{"id":"6f5031612026d83b","repo":"dotnet/wpf","slug":"sr-format-sr-templatenotcollected-methodname","errorCode":null,"errorMessage":"SR.Format(SR.TemplateNotCollected, methodName)","messagePattern":"SR\\.Format\\(SR\\.TemplateNotCollected, methodName\\)","errorType":"exception","errorClass":"XamlInternalException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/DeferredWriter.cs","lineNumber":81,"sourceCode":"        {\n            WriteObject(null, true, \"WriteGetObject\");\n        }\n\n        public override void WriteStartObject(XamlType xamlType)\n        {\n            WriteObject(xamlType, false, \"WriteStartObject\");\n        }\n\n        private void WriteObject(XamlType xamlType, bool fromMember, string methodName)\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, methodName));\n\n                case DeferringMode.TemplateStarting:\n                    StartDeferredList();\n                    _mode = DeferringMode.TemplateDeferring;\n                    goto case DeferringMode.TemplateDeferring;\n\n                case DeferringMode.TemplateDeferring:\n                    if (fromMember)\n                    {\n                        _deferredWriter.WriteGetObject();\n                    }\n                    else\n                    {\n                        _deferredWriter.WriteStartObject(xamlType);\n                    }\n\n                    _deferredTreeDepth += 1;\n                    _handled = true;","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/DeferredWriter.cs#L63-L99","documentation":"XamlInternalException with SR.TemplateNotCollected thrown from DeferredWriter.WriteObject when the writer is in DeferringMode.TemplateReady but WriteStartObject/WriteGetObject was called before a template content was collected. In TemplateReady state the writer expects StartDeferredList to be triggered via the template-starting path, not an arbitrary object write.","triggerScenarios":"Writing a XAML node stream into a deferring writer whose _mode == TemplateReady, with WriteStartObject or WriteGetObjection invoked directly — i.e. the node stream does not begin the template content the deferring reader expects (the expected flow is TemplateStarting -> StartDeferredList -> TemplateDeferring).","commonSituations":"Custom XAML node-stream transformations or rewriters that re-emit nodes around deferred templates (x:XData / ControlTemplate content) in an order that skips the template-collection handshake; piping nodes from a non-deferring reader directly into a deferring writer.","solutions":["Emit the node stream in the order the deferring writer expects so the template start triggers TemplateStarting and StartDeferredList before objects are written","Use XamlDeferringWriter/reader pair through the documented SaveXaml/LoadXaml flow instead of hand-driving the writer","Reset or recreate the DeferredWriter so _mode returns to Off before writing non-template objects","Wrap in try/catch on XamlInternalException during development to surface ordering bugs early"],"exampleFix":"// before\nwriter.WriteStartObject(type);   // writer in TemplateReady state -> TemplateNotCollected\n// after\nwriter.WriteStartMember(templateMember); // let template start put writer into TemplateStarting first\nwriter.WriteStartObject(type);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { deferringWriter.WriteStartObject(type); }\ncatch (XamlInternalException ex) { log.Error(\"DeferredWriter invalid state: {0}\", ex.Message); /* regenerate stream non-deferred */ }","preventionTips":["Only write objects after the deferring writer entered TemplateDeferring","Use the standard XamlDeferringReader/Writer flow instead of hand-driving writes","Keep WriteStartObject/WriteEndObject strictly balanced","Recreate the writer after any internal exception rather than continuing"],"tags":["xaml","internal-exception","deferring-writer","state-machine"],"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-22T01:17:13.364Z"}