Clear platform view's background when MAUI's background is null

This commit is contained in:
Peter Spada 2024-09-10 16:37:48 -07:00
Родитель 9055699a90
Коммит 17c2b20b9f
3 изменённых файлов: 51 добавлений и 36 удалений

Просмотреть файл

@ -335,6 +335,35 @@ namespace Microsoft.Maui.DeviceTests
});
}
[Fact]
public async Task SetBackgroundToNullAffectsRendering()
{
SetupBuilder();
Label label;
var layout = new VerticalStackLayout
{
(label = new Label
{
WidthRequest = 200,
HeightRequest = 100,
Text = "Label",
TextColor = Colors.Red,
Background = new SolidColorBrush(Colors.Blue),
})
};
await AttachAndRun(layout, async (handler) =>
{
var platformView = handler.ToPlatform();
await platformView.AssertContainsColor(Colors.Blue, MauiContext);
label.Background = null;
await platformView.AssertDoesNotContainColor(Colors.Blue, MauiContext);
});
}
[Theory(
#if WINDOWS
Skip = "Fails on Windows"

Просмотреть файл

@ -271,37 +271,30 @@ namespace Microsoft.Maui.Platform
{
var paint = background;
if (!paint.IsNullOrEmpty())
{
// Remove previous background gradient if any
if (platformView.Background is MauiDrawable mauiDrawable)
{
platformView.Background = null;
mauiDrawable.Dispose();
}
// Remove previous background gradient if any
(platformView.Background as MauiDrawable)?.Dispose();
if (treatTransparentAsNull && paint.IsTransparent())
{
// For controls where android treats transparent as null it's more
// performant to just set the background to null instead of
// giving it a transparent color/drawable
platformView.Background = null;
}
else if (paint is SolidPaint solidPaint)
{
if (solidPaint.Color is Color backgroundColor)
platformView.SetBackgroundColor(backgroundColor.ToPlatform());
}
else
{
if (paint!.ToDrawable(platformView.Context) is Drawable drawable)
platformView.Background = drawable;
}
}
else if (platformView is LayoutViewGroup)
if (paint.IsNullOrEmpty())
{
platformView.Background = null;
}
else if (treatTransparentAsNull && paint.IsTransparent())
{
// For controls where android treats transparent as null it's more
// performant to just set the background to null instead of
// giving it a transparent color/drawable
platformView.Background = null;
}
else if (paint is SolidPaint solidPaint)
{
if (solidPaint.Color is Color backgroundColor)
platformView.SetBackgroundColor(backgroundColor.ToPlatform());
}
else
{
if (paint!.ToDrawable(platformView.Context) is Drawable drawable)
platformView.Background = drawable;
}
}
public static void UpdateOpacity(this AView platformView, IView view) => platformView.UpdateOpacity(view.Opacity);

Просмотреть файл

@ -77,14 +77,9 @@ namespace Microsoft.Maui.Platform
if (paint.IsNullOrEmpty())
{
if (platformView is LayoutView)
platformView.BackgroundColor = null;
else
return;
platformView.BackgroundColor = null;
}
if (paint is SolidPaint solidPaint)
else if (paint is SolidPaint solidPaint)
{
Color backgroundColor = solidPaint.Color;
@ -92,8 +87,6 @@ namespace Microsoft.Maui.Platform
platformView.BackgroundColor = ColorExtensions.BackgroundColor;
else
platformView.BackgroundColor = backgroundColor.ToPlatform();
return;
}
else if (paint is GradientPaint gradientPaint)
{