Why does not work RadEntryHandler for RadEntry on teh MAUI

1 Answer 139 Views
Entry
Hakob
Top achievements
Rank 2
Iron
Hakob asked on 14 Jun 2023, 09:42 PM

handlers.AddHandler(typeof(Telerik.Maui.Controls.RadEntry), typeof(RadEntryCustomHandler));

for ANDROID I have this Handler

public partial class RadEntryCustomHandler : RadEntryHandler
{
  protected override void ConnectHandler(RadMauiEntry nativeView)
  {
    try
    {
      nativeView.EditText.TextCursorDrawable.SetColorFilter(new PorterDuffColorFilter(Graphics.Color.White, PorterDuff.Mode.Darken));
    }
    catch { }
  }
}

 

for IOS I have this Handler

public class RadEntryCustomHandler : RadEntryHandler
{
  protected override void ConnectHandler(RadMauiEntry nativeView)
  {
    try
    {
      nativeView.TextField.TintColor = UIColor.White;
    }
    catch { }
  }
}

 

1 Answer, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 15 Jun 2023, 01:43 PM

Hello Hakob,

You do not need to write a custom handler. In fact we recommend against it because you don't always have control over the lifecycle.

Instead, we recommend using the HandlerChanged event and then apply the customization. This will ensure that you have a valid PlatformView and can apply the desired platform-specific customization.

For example, I can confirm this code works and changes the cursor to Red on Android:

using Telerik.Maui.Controls;
using Telerik.Maui.Platform;

#if ANDROID
using Android.Graphics;
#elif __IOS__
using UIKit;
#endif

namespace MauiDemo;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        this.BindingContext = new MainViewModel();
        
        // Step 1. Subscribe to HandlerChanged on your RadEntry
        MyRadEntry.HandlerChanged += Entry1_HandlerChanged;
    }

    private void Entry1_HandlerChanged(object sender, EventArgs e)
    {
        // Step 2. Make sure the PlatformView is available
        if ((sender as RadEntry)?.Handler?.PlatformView is RadMauiEntry nativeEntry)
        {
            // Step 3. change the desired native-specific properties
#if ANDROID
            nativeEntry.EditText.TextCursorDrawable?.SetColorFilter(new PorterDuffColorFilter(Android.Graphics.Color.Red, PorterDuff.Mode.Darken));

#elif __IOS__
        
            nativeEntry.TextField.TintColor = UIColor.White;
#endif
        }
    }
}

Here's what it looks like at runtime:


Side Note

If you want to apply this customization to many instances, then you can create your own custom control that inherits from Telerik.Maui.Controls.RadEntry and use the OnHandlerChanged override:

public class MyCustomTelerikEntry : Telerik.Maui.Controls.RadEntry
{
    protected override void OnHandlerChanged()
    {
        // DO NOT SKIP THIS, or you risk breaking internal logic
        base.OnHandlerChanged();

        // Step 2. Make sure the PlatformView is available
        if (this?.Handler?.PlatformView is RadMauiEntry nativeEntry)
        {
            // Step 3. change the desired native-specific properties
#if ANDROID
            nativeEntry.EditText.TextCursorDrawable?.SetColorFilter(new PorterDuffColorFilter(Android.Graphics.Color.Red, PorterDuff.Mode.Darken));

#elif __IOS__
        
            nativeEntry.TextField.TintColor = UIColor.White;
#endif
        }
    }
}

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Hakob
Top achievements
Rank 2
Iron
commented on 16 Jun 2023, 05:08 AM

Thank you, it is so helpful.
Tags
Entry
Asked by
Hakob
Top achievements
Rank 2
Iron
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or