MAUI: How to migrate a custom render to a handler?

Sreejith Sreenivasan 1,001 Reputation points
2023-10-12T13:43:29.9833333+00:00

I have a custom render in my Xamarin Forms application and I am trying to migrate it to a Handler in my MAUI application.

My custom render is for removing the under line of Entry. Adding it below:

CustomEntry

	public class CustomEntry : Entry
	{
	}

Under android platform:

	public class CustomEntryRenderer : EntryRenderer
	{
		public CustomEntryRenderer(Context context) : base(context)
		{
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
		{
			base.OnElementChanged(e);

			if (Control != null)
			{
				GradientDrawable gd = new GradientDrawable();
				gd.SetColor(global::Android.Graphics.Color.Transparent);
				this.Control.Background = gd;
				this.Control.SetRawInputType(InputTypes.TextFlagNoSuggestions);
			}
		}
	}

Under ios platform:

public class CustomEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.BorderStyle = UITextBorderStyle.None;
        }
    }
}

Above is the existing custom render code. I want to convert it to Handlers and could you show how I can do that? I didn't get a clear idea after reading the this blog.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,071 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,191 Reputation points Microsoft External Staff
    2023-10-13T03:29:16.1133333+00:00

    Hello,

    Handlers can be customized per platform by using conditional compilation, to multi-target code based on the platform.

    Wherefore, you can create a method then put the handler in it. Then get the native platform control by handler.PlatformView, for example, handler.PlatformView is edittext in android. then set the background and the set RawInputType directly. that is same for iOS.

        void ModifyEntry()
        {
            Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
            {
    
               if(view is CustomEntry)
                {
    #if ANDROID
                    Android.Graphics.Drawables.GradientDrawable gradientDrawable = new Android.Graphics.Drawables.GradientDrawable();
                    gradientDrawable.SetColor(global::Android.Graphics.Color.Transparent);
                    handler.PlatformView.Background = gradientDrawable;
                    handler.PlatformView.SetRawInputType(InputTypes.TextFlagNoSuggestions);
    #elif IOS
                handler.PlatformView.BorderStyle = UITextBorderStyle.None;
    #endif
                }
           });
        }
    

    If you want to make this handler in the contentpage. you can call this method in the page's constructor. As note, you use the native platform namespace, please use conditional compilation to wrap these specific platform code.

    #if ANDROID
    using Android.Graphics.Drawables;
    using Android.Text;
    #elif IOS
    using UIKit;
    #endif
    
    namespace MauiApp4;
    public partial class NewPage1 : ContentPage
    {
        public NewPage1()
        {
            InitializeComponent();
           ModifyEntry();
        }
    

    If you want custom control, you can refer to this .NET MAUI control customization with handlers document

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.