dotnet/wpf · error · InvalidOperationException

SR.NestedBeginInitNotSupported

Error message

SR.NestedBeginInitNotSupported

What it means

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.

Solutions

  1. Track initialization state and call EndInit before a subsequent BeginInit
  2. Check your own flag or the element state before calling BeginInit; guard with try/finally so EndInit always runs
  3. Let the XAML loader manage initialization instead of calling BeginInit manually on parsed elements
  4. Refactor shared helper methods so only one owner calls Begin/EndInit per element

Example fix

// before
el.BeginInit(); el.BeginInit(); el.EndInit();
// after
el.BeginInit();
try { /* configure */ }
finally { el.EndInit(); }
Defensive patterns

Strategy: try-catch

Validate before calling

// track your own pending flag per element
bool pending = initPending.GetOrAdd(el, _ => false);
if (pending) throw new InvalidOperationException("nested BeginInit");

Try / catch

try { el.BeginInit(); }
catch (InvalidOperationException) { /* already initializing; skip or await */ }

Prevention

When it happens

Trigger: Calling BeginInit() on an element that already has InitPending set — i.e. a second BeginInit before EndInit.

Common situations: 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.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f8cca36831a4d48b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs:5398

            if (IsKeyboardFocused)
                BringIntoView();

            base.OnGotFocus(e);
        }

        #endregion Input

        #region ISupportInitialize

        /// <summary>
        ///     Initialization of this element is about to begin
        /// </summary>
        public virtual void BeginInit()
        {
            // Nested BeginInits on the same instance aren't permitted
            if (ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);
            }

            // Mark the element as pending initialization
            WriteInternalFlag(InternalFlags.InitPending, true);
        }

        /// <summary>
        ///     Initialization of this element has completed
        /// </summary>
        public virtual void EndInit()
        {
            // Every EndInit must be preceeded by a BeginInit
            if (!ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);
            }

            // Reset the pending flag

View on GitHub (pinned to 81131a70a4)