.NET MAUI Graphics Drawing
Common gotchas
| Issue | Fix |
|---|---|
| Nothing draws on screen | Ensure Drawable is set on GraphicsView and the control has non-zero HeightRequest/WidthRequest |
| State bleeds between shapes | Wrap isolated sections in SaveState() / RestoreState() pairs |
| Shadows stick to later draws | Call canvas.SetShadow(SizeF.Zero, 0, null) after drawing the shadowed element |
| Clipping never resets | Clipping is cumulative per frame β use SaveState/RestoreState around clip regions |
| UI freezes during drawing | Never do I/O, network, or heavy computation inside Draw() β it runs on the UI thread |
Canvas state β always pair Save/Restore
β οΈ Unpaired SaveState/RestoreState causes state leaks across draw calls.
// β
Correct β isolated state
canvas.SaveState();
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawRectangle(10, 10, 80, 80);
canvas.RestoreState();
// Stroke reverts to previous values
// β Wrong β state leaks to everything drawn after
canvas.StrokeColor = Colors.Red;
canvas.StrokeSize = 6;
canvas.DrawRectangle(10, 10, 80, 80);
// Every subsequent shape is now red with size 6Saves/restores: stroke, fill, font, shadow, clip, and transforms. Nest calls for layered isolation.
Triggering redraws
// β
Correct β queue a redraw
graphicsView.Invalidate();
// β Wrong β never call Draw() directly
myDrawable.Draw(canvas, rect);Invalidate()queues a redraw; the framework callsIDrawable.Drawon the next frame.- β οΈ Avoid calling
Invalidate()in a tight loop β batch state changes, then invalidate once.
Performance tips
- Keep
Draw()fast β pre-compute paths and data outside the draw method;Draw()is called on every frame. - Reuse
PathFobjects β create them once, store as fields, draw repeatedly. - Use
MeasureFirstItem-style thinking β if drawing many identical items, calculate dimensions once. - Minimize allocations β avoid
new PathF()insideDraw()when the path doesn't change.
// β
Pre-computed path (field)
private readonly PathF _starPath = BuildStarPath();
public void Draw(ICanvas canvas, RectF dirtyRect)
{
canvas.FillPath(_starPath);
}
// β Allocating every frame
public void Draw(ICanvas canvas, RectF dirtyRect)
{
var path = new PathF();
// ...build path every frame...
canvas.FillPath(path);
}Shadows and clipping β sticky state pitfalls
Shadows apply to all subsequent draws until explicitly removed. Clips accumulate and can only be undone with RestoreState():
// β
Shadow: remove after use
canvas.SetShadow(new SizeF(5, 5), 4, Colors.Gray);
canvas.FillRectangle(20, 20, 100, 60);
canvas.SetShadow(SizeF.Zero, 0, null); // β must remove
// β
Clip: isolate with SaveState/RestoreState
canvas.SaveState();
canvas.ClipRectangle(20, 20, 100, 100);
canvas.FillRectangle(0, 0, 200, 200); // clipped
canvas.RestoreState(); // clip removed
// β Clip persists β everything after is also clipped
canvas.ClipRectangle(20, 20, 100, 100);
canvas.FillEllipse(150, 150, 50, 50); // unintentionally clipped!Set properties BEFORE draw calls
// β
Properties then draw
canvas.StrokeColor = Colors.Blue;
canvas.DrawRectangle(10, 10, 100, 50);
// β Setting after draw has no effect on previous shape
canvas.DrawRectangle(10, 10, 100, 50);
canvas.StrokeColor = Colors.Blue;Decision framework
| Need | Approach |
|---|---|
| Simple shapes / static graphics | Single IDrawable, draw in Draw() |
| Animated graphics | Update state externally, call Invalidate() from timer/animation |
| Complex layered scene | Multiple SaveState/RestoreState blocks, or separate drawables |
| Hit testing on drawn elements | Track shape bounds manually β GraphicsView has no built-in hit test on drawn content |
| Platform-specific rendering | Use handlers/platform code; Microsoft.Maui.Graphics is cross-platform only |
Quick checklist
-
GraphicsViewhasDrawableset and non-zero size -
Draw()is fast β no I/O, no heavy allocations - Every
SaveState()has a matchingRestoreState() - Shadows removed with
SetShadow(SizeF.Zero, 0, null)after use - Clips wrapped in
SaveState/RestoreStateblocks - Properties set before the draw call they apply to
-
Invalidate()used instead of callingDraw()directly