docs-maui
docs-maui copied to clipboard
Handler customisation
Options for customising handlers.
Mappings
Mappings are the simplest option for most behaviour changes. If you want to change how a property affects a native control (or even ignore the property), modifying the mapping for the property is the choice. Existing mappings can be modified/replaced, and you can add new mappings.
Custom factory method
Each handler type provides a factory method (public static Func<T>
) which it uses when creating the native controls. If you wish to use a custom subclass of the native control type in your handler, you can modify the factory method to return the desired type. This should only be used if the customisations you require are not achievable via a mapping - for example, if your custom type requires particular constructor parameters.
Subclassing the native control type
Some native control customisations may not be achievable via public property setters and methods. In these cases, you may need to subclass the native control type. You'll also need to modify the factory method to return your custom type.
Example:
#if IOS
public class WorkaroundMauiTextField : Microsoft.Maui.Platform.MauiTextField
{
public WorkaroundMauiTextField()
: base()
{
BorderStyle = UIKit.UITextBorderStyle.RoundedRect;
ClipsToBounds = true;
}
[Foundation.Export("selectedTextRange")]
public new UIKit.UITextRange SelectedTextRange {
get => base.SelectedTextRange;
set => base.SelectedTextRange = value;
}
}
#endif
Then, in MauiProgram.cs:
#if IOS
Microsoft.Maui.Handlers.EntryHandler.PlatformViewFactory =
handler => new WorkaroundMauiTextField();
#endif
Create your own handler
Customising an existing handler may not be sufficient to achieve your goals. Instead, you may need to create your own handler. This can be necessary when you need a lot of mappings that aren't available on the default handler, or you want completely different behaviour.
Subclass a handler
Don't do this. Instead, log a bug to make an earlier approach possible.
Also, https://github.com/dotnet/docs-maui/issues/2111 for some specifics.