-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Customizing Controls with Handlers
David Ortinau edited this page Apr 21, 2021
·
3 revisions
UI controls in .NET MAUI are rendered via handlers, lightweight classes that map properties and features to bite-sized implementations. Combine that with .NET multi-targeting and you can now succinctly augment any aspect of the native platform control.
The following examples show using compiler directives (#ifdef) to multi-target code based on platform. You can just as easily use platform-specific folders or files to organize such code like
Android\Entry.cs
forEntry
specific customizations, orEntry.android.cs
. The organization is up to you.
This code can live anywhere in your .NET MAUI project.
This would call RandomColor
when setting the BackgroundColor
on any view.
#if ANDROID
Handlers.ViewHandler
.ViewMapper[nameof(IView.BackgroundColor)] = (h, v) =>
{
(h.NativeView as global::Android.Views.View).BackgroundColor
= RandomColor();
};
#endif
#if ANDROID
Handlers.EntryHandler
.EntryMapper[nameof(IEntry.BackgroundColor)] = (h, v) =>
{
(h.NativeView as global::Android.Views.Entry).UnderlineVisible = false;
};
#endif
#if ENABLE_TEST_CLOUD
Handlers.ViewHandler
.ViewMapper[nameof(IView.AutomationId)] = (h, v) =>
{
(h.NativeView as global::Android.Views.View).ContentDescription
= v.AutomationId;
};
#endif