Skip to content
Merged
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue36302.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace Maui.Controls.Sample.Issues;


[Issue(IssueTracker.Github, 36302, "Image and ImageButton backgrounds not cleared on iOS/MacCatalyst when set to null",
PlatformAffected.iOS | PlatformAffected.macOS)]
public class Issue36302 : ContentPage
{
public Issue36302()
{
var image = new Image
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
{
Source = "dotnet_bot.png",
HeightRequest = 80,
WidthRequest = 80,
BackgroundColor = Colors.Blue,
AutomationId = "BackgroundImage",
};

var imageButton = new ImageButton
{
Source = "dotnet_bot.png",
HeightRequest = 80,
WidthRequest = 80,
BackgroundColor = Colors.Blue,
AutomationId = "BackgroundImageButton",
};

var clearButton = new Button
{
Text = "Clear Backgrounds",
AutomationId = "ClearBackgroundsButton",
};
clearButton.Clicked += (s, e) =>
{
image.BackgroundColor = null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[moderate] Regression Prevention and Test Coverage — This test (and the added handler code) only exercises the solid-Color-cleared-to-null scenario from the original report. The new ImageSourcePaint (image-as-background) branch added in ImageHandler.iOS.cs/ImageButtonHandler.iOS.cs MapBackground has no test coverage anywhere in the repo (no unit test or UI test sets Background to an image paint on iOS/MacCatalyst for Image/ImageButton). Consider adding a scenario that sets an image background then clears it, to guard the newly-added code path from regressing silently.

imageButton.BackgroundColor = null;
};

Content = new VerticalStackLayout
{
Spacing = 20,
Margin = new Thickness(20),
Children =
{
image,
imageButton,
clearButton,
}
};
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;
public class Issue36302 : _IssuesUITest
{
public Issue36302(TestDevice testDevice) : base(testDevice) { }

public override string Issue => "Image and ImageButton backgrounds not cleared on iOS/MacCatalyst when set to null";

[Test]
[Category(UITestCategories.Image)]
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
public void ImageAndImageButtonBackgroundClearsWhenSetToNull()
{
App.WaitForElement("ClearBackgroundsButton");
App.Tap("ClearBackgroundsButton");
VerifyScreenshot();
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
}
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
Comment on lines +12 to +19
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Image/ImageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public partial class ImageHandler : IImageHandler
{
public static IPropertyMapper<IImage, IImageHandler> Mapper = new PropertyMapper<IImage, IImageHandler>(ViewHandler.ViewMapper)
{
#if __ANDROID__ || WINDOWS || TIZEN
[nameof(IImage.Background)] = MapBackground,
#endif
#if WINDOWS
[nameof(IImage.Height)] = MapHeight,
[nameof(IImage.Width)] = MapWidth,
#endif
[nameof(IImage.Aspect)] = MapAspect,
[nameof(IImage.IsAnimationPlaying)] = MapIsAnimationPlaying,
#if __ANDROID__ || __IOS__ || MACCATALYST || WINDOWS || TIZEN

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need #if here?

[nameof(IImage.Background)] = MapBackground,
#endif
[nameof(IImage.Source)] = MapSource,
};

Expand Down
25 changes: 24 additions & 1 deletion src/Core/src/Handlers/Image/ImageHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Primitives;
using UIKit;

Expand All @@ -23,7 +24,29 @@ public static void MapBackground(IImageHandler handler, IImage image)
{
handler.UpdateValue(nameof(IViewHandler.ContainerView));

handler.ToPlatform().UpdateBackground(image);
var platformView = handler.ToPlatform();
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved

if (image.Background is ImageSourcePaint imagePaint)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[minor] Handler Mapper and Property Patterns — Same ImageSourcePaint async-loading branch is duplicated here from ViewHandler.MapBackground. Here it's harder to fully delegate since this method deliberately targets handler.ToPlatform() (container-aware) rather than handler.PlatformView, so full base-mapper reuse isn't a drop-in fix — but it does mean the same async-loading logic now exists in three places (base ViewHandler, ImageHandler.iOS, ImageButtonHandler.iOS) that must be kept behaviorally consistent by hand.

{
var provider = handler.GetRequiredService<IImageSourceServiceProvider>();
platformView.UpdateBackgroundImageSourceAsync(imagePaint.ImageSource, provider)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[major] Async and Threading Safety — UpdateBackgroundImageSourceAsync(...).FireAndForget(handler) is unguarded with no ordering/staleness check. Concrete race: set Image.Background to a slow-loading ImageSourcePaint, then immediately set Image.Background = null. The clear branch removes the layer and resets BackgroundColor, but the older async load can later complete and unconditionally insert a stale background layer, silently undoing the user’s clear. The fix should either cancel/version background image loads or verify the current VirtualView.Background/handler state before applying the awaited result; the added regression test only covers BackgroundColor/SolidPaint, not this image-background clear path.

.FireAndForget(handler);
}
else if (image.Background.IsNullOrEmpty())
{
platformView.RemoveBackgroundLayer();
platformView.BackgroundColor = null;
}
else
{
platformView.UpdateBackground(image);
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
}

// After the container transition (wrapper added or removed), re-run the full
// opacity mapper so the container receives view.Opacity and the inner UIImageView
// is reset to alpha 1. Using UpdateOpacity(image) directly would only set the
// container alpha, leaving the inner view at its previous alpha and compounding it.
handler.UpdateValue(nameof(IView.Opacity));
}

public static void MapAspect(IImageHandler handler, IImage image) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/ImageButton/ImageButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class ImageButtonHandler : IImageButtonHandler
[nameof(IButtonStroke.StrokeColor)] = MapStrokeColor,
[nameof(IButtonStroke.CornerRadius)] = MapCornerRadius,
[nameof(IImageButton.Padding)] = MapPadding,
#if ANDROID || WINDOWS
#if ANDROID || WINDOWS || __IOS__ || MACCATALYST
[nameof(IImageButton.Background)] = MapBackground,
#endif
};
Expand Down
26 changes: 26 additions & 0 deletions src/Core/src/Handlers/ImageButton/ImageButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Maui.Graphics;
using UIKit;

namespace Microsoft.Maui.Handlers
Expand Down Expand Up @@ -46,6 +47,31 @@ protected override void DisconnectHandler(UIButton platformView)
SourceLoader.Reset();
}

//TODO : Make it public in NET 11.
internal static void MapBackground(IImageButtonHandler handler, IImageButton imageButton)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[moderate] Public API Surface / Handler Mapper Patterns — This MapBackground is internal, but the equivalent method is public on Android (ImageButtonHandler.Android.cs) and Windows (ImageButtonHandler.Windows.cs). Calling ImageButtonHandler.MapBackground(handler, view) to chain default behavior when overriding the mapper via PrependToMapping/AppendToMapping (a documented extensibility pattern) will compile on Android/Windows but fail on iOS/MacCatalyst specifically. The //TODO: Make it public in NET 11 acknowledges this is deliberate (likely to avoid a mid-cycle public API addition), but it leaves a real cross-platform extensibility gap until then — worth tracking explicitly (e.g. a linked issue) so it isn't dropped.

{
var platformView = handler.PlatformView;

if (platformView is null)
return;

if (imageButton.Background is ImageSourcePaint imagePaint)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[moderate] Handler Mapper and Property Patterns / Complexity Reduction — This re-implements the ImageSourcePaint FireAndForget branch and the generic-paint fallback that already exist in ViewHandler.MapBackground (src/Core/src/Handlers/View/ViewHandler.cs ~L381-397), instead of delegating for the non-empty cases. Since handler.PlatformView is the same target the base mapper already uses (unlike ImageHandler, which needs the container), this could be: if (imageButton.Background.IsNullOrEmpty()) { platformView.RemoveBackgroundLayer(); platformView.BackgroundColor = UIColor.Clear; } else { Microsoft.Maui.Handlers.ViewHandler.MapBackground(handler, imageButton); }. As written, two independent copies of the image-background-loading logic (Image and ImageButton) must now be kept in sync manually — if the shared async-loading path is later hardened (cancellation, disposal guards, new Paint types), this copy won't receive the fix.

{
var provider = handler.GetRequiredService<IImageSourceServiceProvider>();
platformView.UpdateBackgroundImageSourceAsync(imagePaint.ImageSource, provider)
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 AI-Generated Review (multi-model)

[major] Async and Threading Safety — This fire-and-forget image-background load is not ordered against later Background changes. Concrete scenario: set an ImageButton.Background to an ImageSourcePaint backed by a slow stream/URI, then clear it (BackgroundColor = null) before GetImageAsync completes; the null branch removes the layer, but this awaited continuation can still insert the old image layer afterward because UpdateBackgroundImageSourceAsync has no generation/current-background/connected check. The same pattern was added in ImageHandler.iOS.cs:32 (visible when the image keeps a container, e.g. Clip/Shadow), so please guard async completions or centralize versioning before inserting the layer.

.FireAndForget(handler);
}
else if (imageButton.Background.IsNullOrEmpty())
{
platformView.RemoveBackgroundLayer();
platformView.BackgroundColor = UIColor.Clear;
Comment on lines +66 to +67
}
else
{
platformView.UpdateBackground(imageButton);
Comment thread
devanathan-vaithiyanathan marked this conversation as resolved.
Resolved
}
}

public static void MapStrokeColor(IImageButtonHandler handler, IButtonStroke buttonStroke)
{
handler.PlatformView?.UpdateStrokeColor(buttonStroke);
Expand Down
Loading