-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Using Custom Renderers in .NET MAUI
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 extendsContentView
-
PressableViewRenderer.cs
- the Android implementation -
PressableViewRenderer.cs
- the iOS implementation
To use this in .NET MAUI you will:
- Add the files into the appropriate location in your .NET MAUI project(s)
- Modify the "usings" and files
- Configure the renderers in
MauiProgram.cs
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.
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.
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).