{"record":{"id":"b66f04edf2b58030","repo":"dotnet/wpf","slug":"sr-nestedbegininitnotsupported","errorCode":null,"errorMessage":"SR.NestedBeginInitNotSupported","messagePattern":"SR\\.NestedBeginInitNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs","lineNumber":1529,"sourceCode":"                {\n                    throw new InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor);\n                }\n            }\n        }\n\n        #endregion LogicalTree\n\n        #region ISupportInitialize\n\n        /// <summary>\n        ///     Initialization of this element is about to begin\n        /// </summary>\n        public virtual void BeginInit()\n        {\n            // Nested BeginInits on the same instance aren't permitted\n            if (ReadInternalFlag(InternalFlags.InitPending))\n            {\n                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);\n            }\n\n            // Mark the element as pending initialization\n            WriteInternalFlag(InternalFlags.InitPending, true);\n        }\n\n        /// <summary>\n        ///     Initialization of this element has completed\n        /// </summary>\n        public virtual void EndInit()\n        {\n            // Every EndInit must be preceeded by a BeginInit\n            if (!ReadInternalFlag(InternalFlags.InitPending))\n            {\n                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);\n            }\n\n            // Reset the pending flag","sourceCodeStart":1511,"sourceCodeEnd":1547,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs#L1511-L1547","documentation":"FrameworkContentElement.BeginInit throws InvalidOperationException if called while an initialization is already pending (nested BeginInit on the same instance). The element tracks an InitPending internal flag; BeginInit must always be paired one-to-one with EndInit, and re-entrant calls would corrupt that pairing.","triggerScenarios":"Calling BeginInit() twice on the same FrameworkContentElement without an intervening EndInit(), or calling it manually while a XAML/BAML loader is already initializing the element.","commonSituations":"Custom factory/serialization code that calls BeginInit defensively while the XAML loader is also initializing the same object; recursive construction of the same element.","solutions":["Check ReadLocalValue / track your own flag and only call BeginInit if not already pending","Ensure every BeginInit has a matching EndInit before calling BeginInit again (use try/finally)","Remove manual BeginInit calls when the XAML loader already performs initialization for the element"],"exampleFix":"// before\nel.BeginInit();\nConfigure(el);\nel.BeginInit(); // throws: nested\nel.EndInit(); el.EndInit();\n\n// after\nel.BeginInit();\ntry { Configure(el); }\nfinally { el.EndInit(); }","handlingStrategy":"validation","validationCode":"if (initPending) // track with your own bool or a HashSet<object>\n    return;\nel.BeginInit();\ntry { Configure(el); }\nfinally { el.EndInit(); initPending = false; }","typeGuard":"bool IsBeginInitAllowed => !initPending;","tryCatchPattern":"try { el.BeginInit(); Configure(el); }\ncatch (InvalidOperationException ex) { log.Error(\"Nested BeginInit\", ex); }\nfinally { try { el.EndInit(); } catch { } }","preventionTips":["Always pair BeginInit/EndInit with try/finally","Do not call BeginInit when the XAML loader owns initialization","Track pending-init state per element instance"],"tags":["wpf","initialization","invalid-state-transition"],"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"}