dotnet/wpf · error · InvalidOperationException
SR.PropertyIsInitializeOnly (formatted with "Prefix", type…
Error message
SR.PropertyIsInitializeOnly (formatted with "Prefix", type name)
What it means
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.
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.
Example fix
// before var mapping = new XmlNamespaceMapping(); mapping.Prefix = "rss"; // throws // after var mapping = new XmlNamespaceMapping(); ((ISupportInitialize)mapping).BeginInit(); mapping.Prefix = "rss"; ((ISupportInitialize)mapping).EndInit();
Defensive patterns
Strategy: validation
Validate before calling
if (isInitialized) throw new InvalidOperationException("Prefix cannot be set after initialization");
((ISupportInitialize)mapping).BeginInit();
mapping.Prefix = prefix;
((ISupportInitialize)mapping).EndInit(); Try / catch
try { mapping.Prefix = prefix; }
catch (InvalidOperationException ex) { log.Error("Prefix is initialize-only; wrap in BeginInit/EndInit", ex); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Building namespace mappings programmatically instead of in XAML; reconfiguring a mapping that was already parsed from markup; data-binding to Prefix after load.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.PropertyIsInitializeOnly (formatted with "Uri", type…
- SR.Format(SR.PropertyIsInitializeOnly, "DataType"…
- SR.PropertyIsImmutable (formatted with "Prefix", type name)
- SR.PropertyIsImmutable (formatted with "Uri", type name)
- " }} " element found. Expected fixed page element ( }} ).
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/047f2f314822d36f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/XmlNamespaceMapping.cs:46
/// <summary>
/// Constructor for XmlNamespaceMapping
/// </summary>
public XmlNamespaceMapping(string prefix, Uri uri)
{
_prefix = prefix;
_uri = uri;
}
/// <summary>
/// The prefix to be used for this Namespace
/// </summary>
public string Prefix
{
get { return _prefix; }
set
{
if (!_initializing)
throw new InvalidOperationException(SR.Format(SR.PropertyIsInitializeOnly, "Prefix", this.GetType().Name));
if (_prefix != null && _prefix != value)
throw new InvalidOperationException(SR.Format(SR.PropertyIsImmutable, "Prefix", this.GetType().Name));
_prefix = value;
}
}
/// <summary>
/// The Uri to be used for this Namespace,
/// can be declared as an attribute or as the
/// TextContent of the XmlNamespaceMapping markup tag
/// </summary>
public Uri Uri
{
get { return _uri; }
set
{
if (!_initializing)View on GitHub (pinned to 81131a70a4)