Skip to content

Using Custom Renderers in .NET MAUI

David Ortinau edited this page Apr 30, 2022 · 5 revisions

While there are many benefits to using the new handler-mapper pattern, it's still possible to use the custom renderer pattern for customizing at the platform level familiar to Xamarin.Forms developers. To demonstrate using custom renderers in .NET MAUI, let's consider this Xamarin.Forms control PressableView. The control simply exposes pressed and released events based on the platform-specific gestures. The custom renderer implementation is composed of 3 files:

  • PressableView.cs - the cross-platform class that extends ContentView
  • PressableViewRenderer.cs - the Android implementation
  • PressableViewRenderer.cs - the iOS implementation

To use this in .NET MAUI you will:

  1. Add the files into the appropriate location in your .NET MAUI project(s)
  2. Modify the "usings" and files
  3. Configure the renderers in MauiProgram.cs

Add the Files

If you're using the .NET MAUI multi-targeted project, the cross-platform file can be moved to anywhere outside the Platforms folder, and the platform-specific implementation files should be moved to the corresponding Platform folder.

MoveRendererFiles

On the other hand if you're solution has separate projects per-platform, then you would move the platform-specific implementation files into the corresponding projects.

Modify Usings and Files

Any reference to the Xamarin.Forms.* namespaces need to be removed, and then you can resolve the related types to Microsoft.Maui.*. This needs to be done in all files you've added to the .NET MAUI project(s).

Clone this wiki locally