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,16 +271,14 @@ namespace Microsoft.Maui.Platform
{
var paint = background;
if (!paint.IsNullOrEmpty())
{
// Remove previous background gradient if any
if (platformView.Background is MauiDrawable mauiDrawable)
(platformView.Background as MauiDrawable)?.Dispose();
if (paint.IsNullOrEmpty())
{
platformView.Background = null;
mauiDrawable.Dispose();
}
if (treatTransparentAsNull && paint.IsTransparent())
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
@ -298,11 +296,6 @@ namespace Microsoft.Maui.Platform
platformView.Background = drawable;
}
}
else if (platformView is LayoutViewGroup)
{
platformView.Background = null;
}
}
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;
}
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)
{