From 16703cde6594a3b65cf5d528043e480e9a211a91 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Tue, 2 Dec 2025 12:50:47 +0100 Subject: [PATCH] Fix SolidColorBrush.Equals to compare Color values instead of references Fixes #27281 The issue was that SolidColorBrush.Equals used '==' to compare Color objects, which compares references instead of values. This caused infinite loops when using DynamicResource with OnPlatform because OnPlatform creates new Color instances each time, and the equality check would always return false even for identical color values. Changed to use Equals() which properly compares Color values via Color.Equals(). --- src/Controls/src/Core/SolidColorBrush.cs | 2 +- .../Core.UnitTests/SolidColorBrushTests.cs | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Controls/src/Core/SolidColorBrush.cs b/src/Controls/src/Core/SolidColorBrush.cs index 9429bf2b8812..ab4e1a59d957 100644 --- a/src/Controls/src/Core/SolidColorBrush.cs +++ b/src/Controls/src/Core/SolidColorBrush.cs @@ -47,7 +47,7 @@ public override bool Equals(object obj) if (!(obj is SolidColorBrush dest)) return false; - return Color == dest.Color; + return Equals(Color, dest.Color); } /// diff --git a/src/Controls/tests/Core.UnitTests/SolidColorBrushTests.cs b/src/Controls/tests/Core.UnitTests/SolidColorBrushTests.cs index ee771f99ff09..5a7cb764f16a 100644 --- a/src/Controls/tests/Core.UnitTests/SolidColorBrushTests.cs +++ b/src/Controls/tests/Core.UnitTests/SolidColorBrushTests.cs @@ -53,5 +53,30 @@ public void TestDefaultBrushes() Assert.NotNull(white.Color); Assert.Equal(white.Color, Colors.White); } + + [Fact] + // https://github.com/dotnet/maui/issues/27281 + public void SolidColorBrushEqualsComparesColorValues() + { + // Create two Color instances with identical RGBA values but different object references + // This simulates what happens with OnPlatform which creates new Color instances + var color1 = new Color(1.0f, 0.0f, 0.0f, 1.0f); + var color2 = new Color(1.0f, 0.0f, 0.0f, 1.0f); + + // Verify these are different instances + Assert.False(ReferenceEquals(color1, color2)); + + // Verify the Color.Equals method returns true for same values + Assert.True(color1.Equals(color2)); + + // Create SolidColorBrush instances with these colors + var brush1 = new SolidColorBrush(color1); + var brush2 = new SolidColorBrush(color2); + + // This is the bug from issue #27281: SolidColorBrush.Equals uses '==' for Color comparison + // which compares references instead of values, causing infinite loops with DynamicResource + // and OnPlatform because OnPlatform creates new Color instances each time + Assert.True(brush1.Equals(brush2)); + } } } \ No newline at end of file