mauihandler

Is there a way to create a handler for the RoundRectangle (sealed) control?


I'm working to migrate a Xamarin forms app to MAUI, as you probably know if you are reading this, MAUI has quite the list of bugs still pending to be solved. I'm using handlers as a way to workaround them, but I'm currently stuck at trying to workaround this bug: https://github.com/dotnet/maui/issues/14936

I was trying to create a handler for RoundRectangle on Android, to modify the CornerRadius property, but found that the class is sealed.

Is there a way to get around that without having to create my own control implementing all of the methods and properties?

And I know, this is going to be fixed in a better and definite way on the next patch release, however i would still like to learn from knowledgeable folks in this group if this is feasible, for when I encounter a similar scenario.

I have also tried to create a handler for the Border control instead, but I couldn't find a way to modify its RoundRectangle child.


Solution

  • There basically need to be a Handler if you want to make changes to a control. Otherwise you need to override in other ways, like on every platform and use it's specific way to do things, and tie it together with your own handler.

    Regarding RoundRectangle there are a handler for you to use.

    public partial class RoundRectangleHandlerEx : RoundRectangleHandler
    {
        public static new IPropertyMapper<RoundRectangle, RoundRectangleHandler> Mapper = new PropertyMapper<RoundRectangle, RoundRectangleHandler>(RoundRectangleHandler.Mapper)
        {
            [nameof(RoundRectangle.CornerRadius)] = MapCornerRadius
        };
    
        public RoundRectangleHandlerEx() : base(Mapper)
        {
    
        }
    
        public RoundRectangleHandlerEx(IPropertyMapper mapper) : base(mapper ?? Mapper)
        {
    
        }
    
        public new static void MapCornerRadius(IShapeViewHandler handler, RoundRectangle roundRectangle)
        {
            handler.PlatformView?.InvalidateShape(roundRectangle);
        }
    }