AvaloniaUI/Avalonia · error · InvalidOperationException

This API is only available from OnRender

Error message

This API is only available from OnRender

What it means

Thrown by CompositionCustomVisualHandler.VerifyInRender when a render-scoped API (e.g. drawing-context-dependent properties) is accessed outside the OnRender call. VerifyInRender first checks attachment then checks the _inRender flag, which is only true between the start and end of the Render method that calls OnRender.

Source

Thrown at src/Avalonia.Base/Rendering/Composition/CompositionCustomVisualHandler.cs:55

        {
            _inRender = false;
        }
    }

    public abstract void OnRender(ImmediateDrawingContext drawingContext);

    void VerifyAccess()
    {
        if (_host == null)
            throw new InvalidOperationException("Object is not yet attached to the compositor");
        _host.Compositor.VerifyAccess();
    }

    void VerifyInRender()
    {
        VerifyAccess();
        if (!_inRender)
            throw new InvalidOperationException("This API is only available from OnRender");
    }

    protected Vector EffectiveSize
    {
        get
        {
            VerifyAccess();
            return _host!.Size;
        }
    }

    protected TimeSpan CompositionNow
    {
        get
        {
            VerifyAccess();
            return _host!.Compositor.ServerNow;
        }

View on GitHub (pinned to 11c5427268)

Solutions

  1. Move the call inside OnRender so it executes while _inRender is true.
  2. Cache values you need later during OnRender and read the cache elsewhere.
  3. If you need to draw in response to a message, set a flag in OnMessage and act on it in the next OnRender.

Example fix

// before
public override void OnMessage(object m)
{
    if (m is DrawNow) DrawSomething(); // calls a render-scoped API -> throws
}
// after
private bool _pendingDraw;
public override void OnMessage(object m) { if (m is DrawNow) _pendingDraw = true; }
public override void OnRender(DrawingContext ctx) { if (_pendingDraw) DrawSomething(ctx); }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Calling a render-only member (the properties guarded by VerifyInRender) from OnMessage, from a timer callback, or from external code — anywhere the _inRender flag is false.

Common situations: A handler tries to draw or query render state from OnMessage or a property setter; a developer refactors OnRender logic into a helper called outside the render window; a background thread invokes a render-scoped API.

Related errors


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