diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue36933BackgroundCleared.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue36933BackgroundCleared.png new file mode 100644 index 000000000000..d343e935f0b1 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue36933BackgroundCleared.png differ diff --git a/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue36933BackgroundColorSet.png b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue36933BackgroundColorSet.png new file mode 100644 index 000000000000..a5bfa1a2aeb7 Binary files /dev/null and b/src/Controls/tests/TestCases.Android.Tests/snapshots/android/Issue36933BackgroundColorSet.png differ diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue36933.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue36933.cs new file mode 100644 index 000000000000..95b280a00d0d --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue36933.cs @@ -0,0 +1,57 @@ +namespace Maui.Controls.Sample.Issues; + +[Issue(IssueTracker.Github, 36933, "DatePicker and TimePicker Background set to null at runtime does not clear on iOS/MacCatalyst", PlatformAffected.iOS | PlatformAffected.macOS)] +public class Issue36933 : ContentPage +{ + public Issue36933() + { + var datePicker = new DatePicker + { + AutomationId = "TestDatePicker", + Background = new SolidColorBrush(Colors.LightGreen), + Date = new DateTime(2025, 1, 15) + }; + + var timePicker = new TimePicker + { + AutomationId = "TestTimePicker", + Background = new SolidColorBrush(Colors.LightGreen), + Time = new TimeSpan(10, 30, 0) + }; + + var clearButton = new Button + { + Text = "Set Background to Null", + AutomationId = "ClearBackgroundButton", + Command = new Command(() => + { + datePicker.Background = null; + timePicker.Background = null; + }) + }; + + var setColorButton = new Button + { + Text = "Set Background Color", + AutomationId = "SetBackgroundButton", + Command = new Command(() => + { + datePicker.Background = new SolidColorBrush(Colors.LightBlue); + timePicker.Background = new SolidColorBrush(Colors.LightBlue); + }) + }; + + Content = new VerticalStackLayout + { + Spacing = 20, + Padding = 30, + Children = + { + datePicker, + timePicker, + clearButton, + setColorButton + } + }; + } +} diff --git a/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/Issue36933BackgroundCleared.png b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/Issue36933BackgroundCleared.png new file mode 100644 index 000000000000..927e430d3881 Binary files /dev/null and b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/Issue36933BackgroundCleared.png differ diff --git a/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/Issue36933BackgroundColorSet.png b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/Issue36933BackgroundColorSet.png new file mode 100644 index 000000000000..93b45bc5aaae Binary files /dev/null and b/src/Controls/tests/TestCases.Mac.Tests/snapshots/mac/Issue36933BackgroundColorSet.png differ diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue36933.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue36933.cs new file mode 100644 index 000000000000..2b1cf8975bba --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue36933.cs @@ -0,0 +1,36 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue36933 : _IssuesUITest +{ + public Issue36933(TestDevice device) : base(device) + { + } + + public override string Issue => "DatePicker and TimePicker Background set to null at runtime does not clear on iOS/MacCatalyst"; + + [Test] + [Category(UITestCategories.DatePicker)] + public void VerifyDatePickerAndTimePickerBackgroundClearsOnNull() + { + App.WaitForElement("TestDatePicker"); + + Exception? exception = null; + + // Tap clear button to set Background = null — should clear the initial background + App.Tap("ClearBackgroundButton"); + VerifyScreenshotOrSetException(ref exception, "Issue36933BackgroundCleared"); + + // Tap set button to set a new background color + App.Tap("SetBackgroundButton"); + VerifyScreenshotOrSetException(ref exception, "Issue36933BackgroundColorSet"); + + if (exception is not null) + { + throw exception; + } + } +} diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Issue36933BackgroundCleared.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Issue36933BackgroundCleared.png new file mode 100644 index 000000000000..b0deb05c94c2 Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Issue36933BackgroundCleared.png differ diff --git a/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Issue36933BackgroundColorSet.png b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Issue36933BackgroundColorSet.png new file mode 100644 index 000000000000..4d4f138eef4d Binary files /dev/null and b/src/Controls/tests/TestCases.WinUI.Tests/snapshots/windows/Issue36933BackgroundColorSet.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/Issue36933BackgroundCleared.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/Issue36933BackgroundCleared.png new file mode 100644 index 000000000000..46f1cb485544 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/Issue36933BackgroundCleared.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/Issue36933BackgroundColorSet.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/Issue36933BackgroundColorSet.png new file mode 100644 index 000000000000..262ca6619ca4 Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios-26/Issue36933BackgroundColorSet.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/Issue36933BackgroundCleared.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/Issue36933BackgroundCleared.png new file mode 100644 index 000000000000..c4e96715a26a Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/Issue36933BackgroundCleared.png differ diff --git a/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/Issue36933BackgroundColorSet.png b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/Issue36933BackgroundColorSet.png new file mode 100644 index 000000000000..517e8aa6f75b Binary files /dev/null and b/src/Controls/tests/TestCases.iOS.Tests/snapshots/ios/Issue36933BackgroundColorSet.png differ diff --git a/src/Core/src/Handlers/DatePicker/DatePickerHandler.MacCatalyst.cs b/src/Core/src/Handlers/DatePicker/DatePickerHandler.MacCatalyst.cs index 92a9ad578bdb..cfdd6bd901ea 100644 --- a/src/Core/src/Handlers/DatePicker/DatePickerHandler.MacCatalyst.cs +++ b/src/Core/src/Handlers/DatePicker/DatePickerHandler.MacCatalyst.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Foundation; +using Microsoft.Maui.Graphics; using UIKit; using RectangleF = CoreGraphics.CGRect; @@ -341,5 +342,30 @@ void OnValueChanged(object? sender, EventArgs? e) } } } + + //TODO: Make it public in .NET 11. + internal static void MapBackground(IDatePickerHandler handler, IDatePicker datePicker) + { + if (handler.PlatformView is not UIDatePicker platformView) + { + return; + } + + if (datePicker.Background is ImageSourcePaint image) + { + var provider = handler.GetRequiredService(); + platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider) + .FireAndForget(handler); + } + else if (datePicker.Background.IsNullOrEmpty()) + { + platformView.RemoveBackgroundLayer(); + platformView.BackgroundColor = null; + } + else + { + platformView.UpdateBackground(datePicker); + } + } } } diff --git a/src/Core/src/Handlers/DatePicker/DatePickerHandler.cs b/src/Core/src/Handlers/DatePicker/DatePickerHandler.cs index 305eb4ea36d3..0a7c48e8b464 100644 --- a/src/Core/src/Handlers/DatePicker/DatePickerHandler.cs +++ b/src/Core/src/Handlers/DatePicker/DatePickerHandler.cs @@ -25,6 +25,7 @@ public partial class DatePickerHandler : IDatePickerHandler #if ANDROID || WINDOWS [nameof(IDatePicker.Background)] = MapBackground, #elif IOS + [nameof(IDatePicker.Background)] = MapBackground, [nameof(IDatePicker.FlowDirection)] = MapFlowDirection, #endif [nameof(IDatePicker.CharacterSpacing)] = MapCharacterSpacing, diff --git a/src/Core/src/Handlers/DatePicker/DatePickerHandler.iOS.cs b/src/Core/src/Handlers/DatePicker/DatePickerHandler.iOS.cs index c43af953bca9..a34701da75b3 100644 --- a/src/Core/src/Handlers/DatePicker/DatePickerHandler.iOS.cs +++ b/src/Core/src/Handlers/DatePicker/DatePickerHandler.iOS.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Maui.Graphics; using UIKit; namespace Microsoft.Maui.Handlers @@ -185,6 +186,31 @@ public override void DoneClicked() DatePickerHandler.OnDoneClicked(Handler); } } + + //TODO: Make it public in .NET 11. + internal static void MapBackground(IDatePickerHandler handler, IDatePicker datePicker) + { + if (handler.PlatformView is not MauiDatePicker platformView) + { + return; + } + + if (datePicker.Background is ImageSourcePaint image) + { + var provider = handler.GetRequiredService(); + platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider) + .FireAndForget(handler); + } + else if (datePicker.Background.IsNullOrEmpty()) + { + platformView.RemoveBackgroundLayer(); + platformView.BackgroundColor = null; + } + else + { + platformView.UpdateBackground(datePicker); + } + } } #endif } diff --git a/src/Core/src/Handlers/TimePicker/TimePickerHandler.MacCatalyst.cs b/src/Core/src/Handlers/TimePicker/TimePickerHandler.MacCatalyst.cs index 7f74dc9ecd01..c2596c2b2f0f 100644 --- a/src/Core/src/Handlers/TimePicker/TimePickerHandler.MacCatalyst.cs +++ b/src/Core/src/Handlers/TimePicker/TimePickerHandler.MacCatalyst.cs @@ -1,5 +1,6 @@ using System; using Foundation; +using Microsoft.Maui.Graphics; using UIKit; namespace Microsoft.Maui.Handlers @@ -123,5 +124,30 @@ void OnValueChanged(object? sender, EventArgs e) } } } + + //TODO: Make it public in .NET 11. + internal static void MapBackground(ITimePickerHandler handler, ITimePicker timePicker) + { + if (handler.PlatformView is not UIDatePicker platformView) + { + return; + } + + if (timePicker.Background is ImageSourcePaint image) + { + var provider = handler.GetRequiredService(); + platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider) + .FireAndForget(handler); + } + else if (timePicker.Background.IsNullOrEmpty()) + { + platformView.RemoveBackgroundLayer(); + platformView.BackgroundColor = null; + } + else + { + platformView.UpdateBackground(timePicker); + } + } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/TimePicker/TimePickerHandler.cs b/src/Core/src/Handlers/TimePicker/TimePickerHandler.cs index b8b8d030b283..d9ec88df9118 100644 --- a/src/Core/src/Handlers/TimePicker/TimePickerHandler.cs +++ b/src/Core/src/Handlers/TimePicker/TimePickerHandler.cs @@ -18,7 +18,7 @@ public partial class TimePickerHandler : ITimePickerHandler { public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper) { -#if ANDROID || WINDOWS +#if IOS || ANDROID || WINDOWS [nameof(ITimePicker.Background)] = MapBackground, #endif #if IOS || ANDROID diff --git a/src/Core/src/Handlers/TimePicker/TimePickerHandler.iOS.cs b/src/Core/src/Handlers/TimePicker/TimePickerHandler.iOS.cs index caddf83802b7..f1b2c4eed31e 100644 --- a/src/Core/src/Handlers/TimePicker/TimePickerHandler.iOS.cs +++ b/src/Core/src/Handlers/TimePicker/TimePickerHandler.iOS.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Maui.Graphics; namespace Microsoft.Maui.Handlers { @@ -156,6 +157,31 @@ public void OnDateSelected() } } } + + //TODO: Make it public in .NET 11. + internal static void MapBackground(ITimePickerHandler handler, ITimePicker timePicker) + { + if (handler.PlatformView is not MauiTimePicker platformView) + { + return; + } + + if (timePicker.Background is ImageSourcePaint image) + { + var provider = handler.GetRequiredService(); + platformView.UpdateBackgroundImageSourceAsync(image.ImageSource, provider) + .FireAndForget(handler); + } + else if (timePicker.Background.IsNullOrEmpty()) + { + platformView.RemoveBackgroundLayer(); + platformView.BackgroundColor = null; + } + else + { + platformView.UpdateBackground(timePicker); + } + } } #endif } \ No newline at end of file