0

I would like to customize a Picker as well as DatePicker in .NET MAUI using handlers for Android. I want the dialog to have rounded corners to match the overall style of my application.

I have tried to follow the Microsoft documentation for handlers and have the following code thus far.

Microsoft.Maui.Handlers.PickerHandler.Mapper.AppendToMapping("RoundedCorners", (handler, view) =>
        {
#if ANDROID
            handler.PlatformView.BackgroundTintList = ColorStateList.ValueOf(Colors.Transparent.ToAndroid());


            handler.PlatformView.SetBackgroundResource( ??? )
                        
            
#elif IOS

#endif
        });

I figure that you can customize the background, which if I am correct is a basic dialog, using the SetBackgroundResource() method. However, I don't know how to correctly use this method. I assume, one needs to instantiate a new dialog with and reference it in the method?

I have also tried the following approach but it did not work:

    Microsoft.Maui.Handlers.ContentViewHandler.Mapper.AppendToMapping("Dialog", (handler, view) =>
        {
#if ANDROID
            var dialog = new Dialog(Android.App.Application.Context);

            try
            {
                dialog = view as Dialog;
            }

            catch (Exception ex) { }

            if (dialog != null)
            {
                dialog.Window.SetBackgroundBlurRadius(30);
                dialog.Window.SetBackgroundDrawable(GetDrawable());
            }
            
#elif IOS

#endif
        });
FabianK
  • 23
  • 6

1 Answers1

0

For this,you can check Customizing Controls with Handlers.

You can set the BackgroundColor on any view with following code:

#if ANDROID
Microsoft.Maui.Handlers.ViewHandler.ViewMapper.AppendToMapping(nameof(IView.Background), (h, v) =>
    {
    (h.NativeView as global::Android.Views.View).SetBackgroundColor(RandomColor());

    });
#endif

And you can also set the SetBackgroundResource with following code:

 (h.NativeView as global::Android.Views.View).SetBackgroundResource(Resource.Drawable.image));

And for how to custom picker ,you can refer threads :

.NET MAUI: Customize datepicker so that it opens when clicking on an icon

and

.NET Maui Custom Handlers not working.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • Thank you for your answer. Please correct me if I am wrong, but I believe the documentation you are referring to is outdated. For example, .NativeView is replaced by PlatformView and so on in the current documentation. Also - I tried the SetBackgroundResource() method but it did not work for me. I believe I need to access/reference the dialog windows that opens upon clicking the picker but I've failed to to so. Might you have a working example for this? – FabianK Mar 24 '23 at 08:52