1

Maybe I am looking at this in the wrong way but I cannot find information about what how and when to use what, to convert a (Label)renderer to (Label)handler.

Let's take this example, this is what worked in Xamarin.Forms:

public class StrikeThroughLabelRenderer : LabelRenderer
    {
        public StrikeThroughLabelRenderer(global::Android.Content.Context context)
            : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null && Element != null)
            {
                Control.Text += ' ';
                Control.PaintFlags = Control.PaintFlags | PaintFlags.StrikeThruText;
            }
        }
    }

Here you can see a simple renderer which uses 'Element' and checks if it isn't null. How and where do I find the correct way to convert things like this, as I cannot find information..

this is what I now have, in Maui:

  public class StrikeThroughLabelHandler : LabelHandler
    {
        protected override void ConnectHandler(AppCompatTextView platformView)
        {
            base.ConnectHandler(platformView);


            // && Element != null) TO ADD 
            if (platformView != null)
            {
                platformView.Text += ' ';
                platformView.PaintFlags = platformView.PaintFlags 
                                          | PaintFlags.StrikeThruText;
            }
        }
    }

Also when doing this:

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

            var label = (TextView)Control;
            var status = ((BackgroundLabel)e.NewElement).Status;
        }

I can do e. but using the handler I have no way to do this, not that I know of atleast..

Thanks in advance

Ramttid
  • 33
  • 5
  • You are using Entity (dbContext). See : https://learn.microsoft.com/en-us/azure/remote-rendering/concepts/entities?force_isolation=true – jdweng Mar 22 '23 at 13:31
  • 1
    I like this guy: https://www.syncfusion.com/blogs/post/how-to-reuse-xamarin-forms-custom-renderers-in-net-maui.aspx He writes many articles about making MAUI controls. (works at Syncfusion). This is another guy who is good at customizations: https://vladislavantonyuk.github.io/articles/ There are more. You just have to search. But when you read 2-3 articles, you will get what is going on. – H.A.H. Mar 22 '23 at 17:39
  • @H.A.H. but Renderer is obsolete, that's why I'm trying to use Handler, will check him out thanks! – Ramttid Mar 23 '23 at 08:27

2 Answers2

0

You can consider continuing to use Custom Renderer in Maui, you only need to add the custom renderer code to the appropriate position in the .NET MAUI project, and the difference from Xamarin.Forms is that the ExportRenderer property is not required in .NET MAUI (requires delete it). Finally register the renderer in MauiProgram.cs, like this:

     .ConfigureMauiHandlers((handlers) =>
     {
#if ANDROID
     handlers.AddHandler(typeof(PressableView), typeof(XamarinCustomRenderer.Droid.Renderers.PressableViewRenderer));
#elif IOS
     handlers.AddHandler(typeof(PressableView), typeof(XamarinCustomRenderer.iOS.Renderers.PressableViewRenderer));
#endif
     });

For more info, you could refer to Use custom renderers in .NET MAUI and sample code

Zack
  • 1,255
  • 1
  • 2
  • 5
  • But when I use lets say LabelRenderer, it is marked as deprecated – Ramttid Mar 23 '23 at 12:38
  • When you use Renderer in Maui, you will be prompted to deprecate, because Maui provides Handler, but you can use Renderer normally. – Zack Mar 24 '23 at 05:17
0

Initialization in a handler should be done in the ConnectHandler method.

See also Porting Custom Renderers to Handlers and Create a custom control using handlers:

protected override void ConnectHandler(NativeControl platformView)
{
  //initialize

  //subscribe to events

  base.ConnectHandler(platformView);
}
Benl
  • 2,777
  • 9
  • 13