Skip to content

Commit

Permalink
[C] respect specificity while overriding DynRes (#24306)
Browse files Browse the repository at this point in the history
fixes an unreported issue
  • Loading branch information
StephaneDelcroix authored Aug 19, 2024
1 parent 73c2987 commit 87ddfcc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Controls/src/Core/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,8 @@ internal void OnResourcesChanged(IEnumerable<KeyValuePair<string, object>> value
internal override void OnSetDynamicResource(BindableProperty property, string key, SetterSpecificity specificity)
{
base.OnSetDynamicResource(property, key, specificity);
DynamicResources[property] = (key, specificity);
if (!DynamicResources.TryGetValue(property, out var existing) || existing.Item2.CompareTo(specificity) < 0)
DynamicResources[property] = (key, specificity);
if (this.TryGetResource(key, out var value))
OnResourceChanged(property, value, specificity);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Unreported010.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Unreported010">
<ContentPage.Resources>
<Color x:Key="Foo">Blue</Color>
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="{DynamicResource Foo}"/>
</Style>
</ContentPage.Resources>

<VerticalStackLayout>
<Button x:Name="button0" Text="Hello World" BackgroundColor="{DynamicResource Foo}"/>
</VerticalStackLayout>
</ContentPage>
47 changes: 47 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Unreported010.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Unreported010
{
public Unreported010()
{
InitializeComponent();
}

public Unreported010(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void LocalDynamicResources([Values(false, true)] bool useCompiledXaml)
{
var page = new Unreported010(useCompiledXaml);
Assert.That(page.button0.BackgroundColor, Is.EqualTo(Colors.Blue));
page.Resources["Foo"] = Colors.Red;
Assert.That(page.button0.BackgroundColor, Is.EqualTo(Colors.Red));
}
}


}

0 comments on commit 87ddfcc

Please sign in to comment.