{"record":{"id":"40da7859a054374a","repo":"AvaloniaUI/Avalonia","slug":"this-api-is-only-available-from-onrender","errorCode":null,"errorMessage":"This API is only available from OnRender","messagePattern":"This API is only available from OnRender","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Rendering/Composition/CompositionCustomVisualHandler.cs","lineNumber":55,"sourceCode":"        {\n            _inRender = false;\n        }\n    }\n\n    public abstract void OnRender(ImmediateDrawingContext drawingContext);\n\n    void VerifyAccess()\n    {\n        if (_host == null)\n            throw new InvalidOperationException(\"Object is not yet attached to the compositor\");\n        _host.Compositor.VerifyAccess();\n    }\n\n    void VerifyInRender()\n    {\n        VerifyAccess();\n        if (!_inRender)\n            throw new InvalidOperationException(\"This API is only available from OnRender\");\n    }\n\n    protected Vector EffectiveSize\n    {\n        get\n        {\n            VerifyAccess();\n            return _host!.Size;\n        }\n    }\n\n    protected TimeSpan CompositionNow\n    {\n        get\n        {\n            VerifyAccess();\n            return _host!.Compositor.ServerNow;\n        }","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Rendering/Composition/CompositionCustomVisualHandler.cs#L37-L73","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Move the call inside OnRender so it executes while _inRender is true.","Cache values you need later during OnRender and read the cache elsewhere.","If you need to draw in response to a message, set a flag in OnMessage and act on it in the next OnRender."],"exampleFix":"// before\npublic override void OnMessage(object m)\n{\n    if (m is DrawNow) DrawSomething(); // calls a render-scoped API -> throws\n}\n// after\nprivate bool _pendingDraw;\npublic override void OnMessage(object m) { if (m is DrawNow) _pendingDraw = true; }\npublic override void OnRender(DrawingContext ctx) { if (_pendingDraw) DrawSomething(ctx); }","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call render-scoped APIs only inside OnRender.","Cache values during OnRender for use outside the render window.","Convert message-driven draw requests into a flag acted on in the next OnRender."],"tags":["rendering","composition","invalid-operation","render-scope","custom-visual"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}