{"record":{"id":"047f2f314822d36f","repo":"dotnet/wpf","slug":"sr-propertyisinitializeonly-formatted-with-prefix-type-name","errorCode":null,"errorMessage":"SR.PropertyIsInitializeOnly (formatted with \"Prefix\", type name)","messagePattern":"SR\\.PropertyIsInitializeOnly \\(formatted with \"Prefix\", type name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs","lineNumber":46,"sourceCode":"        /// <summary>\n        /// Constructor for XmlNamespaceMapping\n        /// </summary>\n        public XmlNamespaceMapping(string prefix, Uri uri)\n        {\n            _prefix = prefix;\n            _uri = uri;\n        }\n\n        /// <summary>\n        /// The prefix to be used for this Namespace\n        /// </summary>\n        public string Prefix\n        {\n            get { return _prefix; }\n            set\n            {\n                if (!_initializing)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsInitializeOnly, \"Prefix\", this.GetType().Name));\n                if (_prefix != null && _prefix != value)\n                    throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, \"Prefix\", this.GetType().Name));\n\n                _prefix = value;\n            }\n        }\n\n        /// <summary>\n        /// The Uri to be used for this Namespace,\n        /// can be declared as an attribute or as the\n        /// TextContent of the XmlNamespaceMapping markup tag\n        /// </summary>\n        public Uri Uri\n        {\n            get { return _uri; }\n            set\n            {\n                if (!_initializing)","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs#L28-L64","documentation":"XmlNamespaceMapping.Prefix is initialize-only: it can only be set while the object is inside its ISupportInitialize.BeginInit/EndInit window (e.g. during XAML parsing). Setting Prefix after initialization completes throws an InvalidOperationException formatted with the property name and the concrete type name.","triggerScenarios":"Assigning mapping.Prefix from code after the mapping was created outside a BeginInit()/EndInit() scope, e.g. `var m = new XmlNamespaceMapping(); m.Prefix = \"x\";` because the parameterless constructor does not enter the initializing state.","commonSituations":"Building namespace mappings programmatically instead of in XAML; reconfiguring a mapping that was already parsed from markup; data-binding to Prefix after load.","solutions":["Create the mapping in XAML where the initialize-only protocol is honored.","In code, call BeginInit() before setting Prefix/Uri and EndInit() afterwards.","Set both values via constructor arguments or in an initialization block, not after the mapping is in use."],"exampleFix":"// before\nvar mapping = new XmlNamespaceMapping();\nmapping.Prefix = \"rss\"; // throws\n// after\nvar mapping = new XmlNamespaceMapping();\n((ISupportInitialize)mapping).BeginInit();\nmapping.Prefix = \"rss\";\n((ISupportInitialize)mapping).EndInit();","handlingStrategy":"validation","validationCode":"if (isInitialized) throw new InvalidOperationException(\"Prefix cannot be set after initialization\");\n((ISupportInitialize)mapping).BeginInit();\nmapping.Prefix = prefix;\n((ISupportInitialize)mapping).EndInit();","typeGuard":null,"tryCatchPattern":"try { mapping.Prefix = prefix; }\ncatch (InvalidOperationException ex) { log.Error(\"Prefix is initialize-only; wrap in BeginInit/EndInit\", ex); }","preventionTips":["Always set initialize-only properties inside BeginInit/EndInit.","Prefer the (prefix, uri) constructor over property assignments.","Do not bind UI to Prefix/Uri after the mapping is loaded."],"tags":["wpf","xml","initialization","immutable-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-22T01:17:13.364Z"}