{"record":{"id":"f8cca36831a4d48b","repo":"dotnet/wpf","slug":"sr-nestedbegininitnotsupported-frameworkelement","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/FrameworkElement.cs","lineNumber":5398,"sourceCode":"            if (IsKeyboardFocused)\n                BringIntoView();\n\n            base.OnGotFocus(e);\n        }\n\n        #endregion Input\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":5380,"sourceCodeEnd":5416,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L5380-L5416","documentation":"FrameworkElement.BeginInit marks the element as pending initialization (ISupportInitialize). Nested BeginInit calls on the same instance are not supported, so calling BeginInit twice without an intervening EndInit throws InvalidOperationException(SR.NestedBeginInitNotSupported). The pattern is strictly one BeginInit followed by one EndInit.","triggerScenarios":"Calling BeginInit() on an element that already has InitPending set — i.e. a second BeginInit before EndInit.","commonSituations":"Nested ISupportInitialize handling in XAML loaders or custom serialization code that calls BeginInit on elements already being initialized by the parser; helper code calling BeginInit defensively without tracking state.","solutions":["Track initialization state and call EndInit before a subsequent BeginInit","Check your own flag or the element state before calling BeginInit; guard with try/finally so EndInit always runs","Let the XAML loader manage initialization instead of calling BeginInit manually on parsed elements","Refactor shared helper methods so only one owner calls Begin/EndInit per element"],"exampleFix":"// before\nel.BeginInit(); el.BeginInit(); el.EndInit();\n// after\nel.BeginInit();\ntry { /* configure */ }\nfinally { el.EndInit(); }","handlingStrategy":"try-catch","validationCode":"// track your own pending flag per element\nbool pending = initPending.GetOrAdd(el, _ => false);\nif (pending) throw new InvalidOperationException(\"nested BeginInit\");","typeGuard":null,"tryCatchPattern":"try { el.BeginInit(); }\ncatch (InvalidOperationException) { /* already initializing; skip or await */ }","preventionTips":["Always pair Begin/EndInit in try/finally","Never call BeginInit on elements owned by the XAML loader","Centralize initialization in one helper that owns the pair"],"tags":["wpf","initialization","invalid-operation"],"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"}