AvaloniaUI/Avalonia · error · InvalidOperationException

The control isn't currently attached to a toplevel

Error message

The control isn't currently attached to a toplevel

What it means

AndroidNativeControlHostImpl's per-control holder requires _attachedTo (the parent TopLevel host) to be set before ShowInBounds places a native child view. If a child is asked to show before/after attach (or after detach), _attachedTo is null and it throws InvalidOperationException.

Source

Thrown at src/Android/Avalonia.Android/Platform/AndroidNativeControlHostImpl.cs:124

            public bool IsCompatibleWith(INativeControlHostImpl host) => host is AndroidNativeControlHostImpl;

            public void HideWithSize(Size size)
            {
                CheckDisposed();
                if (_attachedTo == null)
                    return;

                size *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;
                _view.Visibility = ViewStates.Gone;
                _view.LayoutParameters = new FrameLayout.LayoutParams(Math.Max(1, (int)size.Width), Math.Max(1, (int)size.Height));
                _view.RequestLayout();
            }

            public void ShowInBounds(Rect bounds)
            {
                CheckDisposed();
                if (_attachedTo == null)
                    throw new InvalidOperationException("The control isn't currently attached to a toplevel");

                bounds *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;
                _view.Visibility = ViewStates.Visible;
                _view.LayoutParameters = new FrameLayout.LayoutParams(Math.Max(1, (int)bounds.Width), Math.Max(1, (int)bounds.Height))
                {
                    LeftMargin = (int)bounds.X,
                    TopMargin = (int)bounds.Y
                };
                _view.RequestLayout();
            }
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Only show/layout native children after the control host is attached to a TopLevel (e.g. in AttachedToVisualTree).
  2. Detach/stop layout when the host detaches so no ShowInBounds runs post-detach.
  3. Guard with `if (_attachedTo is null) return;` at call sites during transition windows.
  4. Audit control lifetime so children are not measured while detached.

Example fix

// before
public void ShowInBounds(Rect bounds)
{
    CheckDisposed();
    if (_attachedTo == null)
        throw new InvalidOperationException("The control isn't currently attached to a toplevel");
    bounds *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;
    ...
}

// after (no-op until attached; attach/detach driven by host lifecycle)
public void ShowInBounds(Rect bounds)
{
    CheckDisposed();
    if (_attachedTo is null) return; // not attached yet — layout will run on attach
    bounds *= _attachedTo._avaloniaView.TopLevelImpl.RenderScaling;
    ...
}
Defensive patterns

Strategy: validation

Validate before calling

// only show children when attached
if (_attachedTo is null) return;

Type guard

static bool IsAttached<TH>(TH host) where TH : class => host is not null;

Try / catch

// guard call sites instead of catching; drive ShowInBounds off attach/detach.

Prevention

When it happens

Trigger: Calling ShowInBounds (or any layout call) on a hosted native Android child view while the control host is not attached to a TopLevel — before Attach or after Detach, or when the TopLevel was torn down.

Common situations: Triggering native control layout during early init before the host is attached; re-laying out controls after the window closed; controls reparented at runtime; calling ShowInBounds from a detached control host.

Related errors


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