AvaloniaUI/Avalonia · error · InvalidOperationException

BeginInit was not called.

Error message

BeginInit was not called.

What it means

Error "BeginInit was not called." thrown in AvaloniaUI/Avalonia.

Source

Thrown at src/Avalonia.Base/StyledElement.cs:359

        /// <inheritdoc/>
        IStyleHost? IStyleHost.StylingParent => (IStyleHost?)InheritanceParent;

        internal static ControlTheme InvalidTheme
            => s_invalidTheme ??= new();

        /// <inheritdoc/>
        public virtual void BeginInit()
        {
            ++_initCount;
        }

        /// <inheritdoc/>
        public virtual void EndInit()
        {
            if (_initCount == 0)
            {
                throw new InvalidOperationException("BeginInit was not called.");
            }

            if (--_initCount == 0 && _logicalRoot is not null)
            {
                ApplyStyling();
                InitializeIfNeeded();
            }
        }

        /// <summary>
        /// Applies styling to the control if the control is initialized and styling is not
        /// already applied.
        /// </summary>
        /// <remarks>
        /// The styling system will automatically apply styling when required, so it should not
        /// usually be necessary to call this method manually.
        /// </remarks>
        /// <returns>

View on GitHub (pinned to 11c5427268)

Solutions

  1. Call BeginInit() before setting properties on the element and EndInit() afterwards.
  2. Remove the EndInit() call if initialization was never started.

When it happens

Trigger: Raised when EndInit is called without a preceding BeginInit on a StyledElement. Defend by always pairing BeginInit/EndInit calls (typically via the initialization pattern in XAML loaders) and never calling EndInit directly.

Common situations: See trigger scenarios.


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/858f514dcd8adbec. Report an issue: GitHub.