{"record":{"id":"32a51838fab33911","repo":"dotnet/wpf","slug":"invalidoperationexception-sr-format-sr-32a518","errorCode":null,"errorMessage":"InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteStartMember\")) [content property, non-empty frame]","messagePattern":"InvalidOperationException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteStartMember\"\\)\\) \\[content property, non-empty frame\\]","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1048,"sourceCode":"                if ((property == XamlLanguage.Items && parentType is not null && parentType.IsWhitespaceSignificantCollection) ||\n                    (property == XamlLanguage.UnknownContent))\n                {\n                    writer.isFirstElementOfWhitespaceSignificantCollection = true;\n                }\n\n                XamlType containingType = writer.namespaceScopes.Peek().Type;\n                if (IsImplicit(property))\n                {\n                    if (!writer.namespaceScopes.Peek().IsEmpty())\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteStartMember\"));\n                    }\n                }\n                else if (property == containingType.ContentProperty)\n                {\n                    if (!writer.namespaceScopes.Peek().IsEmpty())\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteStartMember\"));\n                    }\n                    else\n                    {\n                        writer.currentState = TryContentProperty.State;\n                        return;\n                    }\n                }\n                else\n                {\n                    WriteMemberAsElement(writer);\n                    writer.WriteDeferredNamespaces(XamlNodeType.StartMember);\n                }\n\n                if (property == XamlLanguage.PositionalParameters)\n                {\n                    // the writer is not in a state where it can write markup extensions in curly form\n                    // so it expands the positional parameters as properties.\n","sourceCodeStart":1030,"sourceCodeEnd":1066,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1030-L1066","documentation":"For a content property (property == containingType.ContentProperty), XamlXmlWriter switches to TryContentProperty state, which is only possible when the current frame is empty. If WriteStartMember is called for the content property while another member framing is active (non-empty namespaceScope frame), it throws InvalidOperationException 'WriteStartMember not supported in current state' because content-property shorthand cannot be nested inside an open member.","triggerScenarios":"WriteStartMember with the type's ContentProperty while a previous member in the same object frame has not been ended (namespaceScopes.Peek().IsEmpty() == false).","commonSituations":"Serializing objects where a regular member was opened before the content property without WriteEndMember; custom writers that emit content property inside attribute-like framing; generated code writing members out of order.","solutions":["End the open member with WriteEndMember before starting the content property","Reorder emission so the content property is written after all regular members are closed","Generate the stream via XamlObjectReader to guarantee legal member ordering"],"exampleFix":"// before\nwriter.WriteStartMember(other);\nwriter.WriteStartMember(contentProp); // non-empty frame -> throws\n// after\nwriter.WriteStartMember(other);\nwriter.WriteEndMember();\nwriter.WriteStartMember(contentProp);","handlingStrategy":"try-catch","validationCode":"if (property == containingType.ContentProperty && !currentFrameIsEmpty) throw new InvalidOperationException(\"Content property requires empty frame\");","typeGuard":"bool CanStartContentProperty(XamlMember p, XamlType t, bool frameEmpty) => p != t.ContentProperty || frameEmpty;","tryCatchPattern":"try { writer.WriteStartMember(property); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteStartMember\")) { /* end open member first, then retry */ }","preventionTips":["Write content properties last, after closing all other members","Track open-member state per object frame","Test serialization of types with [ContentProperty] attributes"],"tags":["xaml","content-property","invalid-state"],"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"}