SkiaSharp and MVVM
A common question with SkiaSharp is how to get drawing logic out of the PaintSurface event handler and into a view model. David Britch shares his preferred approach for a clean MVVM setup in .NET MAUI.
What you’ll learn
- Subclassing SKCanvasView — adding a
CanvasRendererbindable property that points at a rendering service - Defining a service interface — an
IBitmapRendererServiceexposingPaintSurface,InvalidateSurface, and an invalidation event - Implementing the service — drawing an
SKBitmapand raising redraw requests from a bindableBitmapproperty - Injecting into a view model — registering the service and consuming it via dependency injection
- Forcing redraws — calling
InvalidateSurfacefrom the view model to keep code-behind empty
Read the full post for the full control, interface, and service code, and a link to a working sample repo.