[Android] Allow custom background drawable with a custom handler#22573
Conversation
# Conflicts: # src/Controls/tests/TestCases.HostApp/Issues/Issue18720.xaml # src/Controls/tests/TestCases.HostApp/Issues/Issue18720.xaml.cs # src/Controls/tests/TestCases.HostApp/Issues/Issue18720DatePicker.xaml # src/Controls/tests/TestCases.HostApp/Issues/Issue18720DatePicker.xaml.cs # src/Controls/tests/TestCases.HostApp/Issues/Issue18720Editor.xaml # src/Controls/tests/TestCases.HostApp/Issues/Issue18720Editor.xaml.cs # src/Controls/tests/TestCases.HostApp/Issues/Issue18720TimePicker.xaml # src/Controls/tests/TestCases.HostApp/Issues/Issue18720TimePicker.xaml.cs
|
I updated this PR to not need a flag but rather use a know type of drawable. If the drawable used in the background is a Maui*Drawable, then we know that we added it and can remove it. Otherwise, we don't do anything since the only alternative is that it was the user. There is the case where the cross-platform view has a background set, then we keep existing behavior and always override the background with the one set in the view. This means that if you have a custom handler that is going things AND set the background in XAML, the XAML will still win and override your custom background. However, if you are trying to replace the background, make sure you are replacing the mapper instead of appending since you may be doubling the work needed to set the background (first maui code then your code): builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
EntryHandler.Mapper.ReplaceMapping<IEntry, IEntryHandler>("Background", (handler, view) =>
{
var platformView = handler.PlatformView;
// Android will reset the padding when setting a Background drawable, so we need to reapply the padding after
var padLeft = platformView.PaddingLeft;
var padTop = platformView.PaddingTop;
var padRight = platformView.PaddingRight;
var padBottom = platformView.PaddingBottom;
// Set the custom background
var gd = new Android.Graphics.Drawables.GradientDrawable();
gd.SetCornerRadius(3);
gd.SetStroke(1, Android.Graphics.Color.Red);
gd.SetColor(Android.Graphics.Color.Pink);
platformView.Background = gd;
// Apply previous padding
platformView.SetPadding(padLeft, padTop, padRight, padBottom);
});
#endif
}); |
|
/azp run MAUI-UITests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Description of Change
Allow custom background drawable with a custom handler on Android.
Note, as in the previous gif, we can add/remove the
BackgroundColororBackgroundin addition to being able to customize the control with a custom handler, removing the background or setting a custom drawable.Issues Fixed
Fixes #18720