diff --git a/src/Controls/src/Core/AppThemeBinding.cs b/src/Controls/src/Core/AppThemeBinding.cs index 7f45c85e1702..39e03611df54 100644 --- a/src/Controls/src/Core/AppThemeBinding.cs +++ b/src/Controls/src/Core/AppThemeBinding.cs @@ -1,6 +1,7 @@ #nullable disable using System; using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Controls.Internals; namespace Microsoft.Maui.Controls { @@ -61,7 +62,13 @@ void ApplyCore(bool dispatch = false) else Set(); - void Set() => target.SetValueCore(_targetProperty, GetValue(), Internals.SetValueFlags.ClearDynamicResource, BindableObject.SetValuePrivateFlags.Default | BindableObject.SetValuePrivateFlags.Converted, specificity); + void Set() { + var value = GetValue(); + if (value is DynamicResource dynamicResource) + target.SetDynamicResource(_targetProperty, dynamicResource.Key, specificity); + else + target.SetValueCore(_targetProperty, value, Internals.SetValueFlags.ClearDynamicResource, BindableObject.SetValuePrivateFlags.Default | BindableObject.SetValuePrivateFlags.Converted, specificity); + }; } object _light; diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml new file mode 100644 index 000000000000..9a2461b5e1fd --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml @@ -0,0 +1,20 @@ + + + + DarkGray + HotPink + Yellow + + + \ No newline at end of file diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml.cs new file mode 100644 index 000000000000..c2e296c5e220 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml.cs @@ -0,0 +1,39 @@ +using System; +using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Controls.Core.UnitTests; +using Microsoft.Maui.Graphics; +using Microsoft.Maui.Devices; +using NUnit.Framework; + +namespace Microsoft.Maui.Controls.Xaml.UnitTests +{ + + + public partial class Maui13619 : ContentPage + { + public Maui13619() => InitializeComponent(); + public Maui13619(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] public void Setup() => AppInfo.SetCurrent(new MockAppInfo()); + [TearDown] public void TearDown() => AppInfo.SetCurrent(null); + + [Test] + public void AppThemeBindingAndDynamicResource([Values(false, true)] bool useCompiledXaml) + { + var page = new Maui13619(useCompiledXaml); + Assert.That(page.label0.TextColor, Is.EqualTo(Colors.HotPink)); + Assert.That(page.label0.BackgroundColor, Is.EqualTo(Colors.DarkGray)); + + page.Resources["Primary"] = Colors.SlateGray; + Assert.That(page.label0.BackgroundColor, Is.EqualTo(Colors.SlateGray)); + + } + } + } +}