From 01fc9798d4f3b2b4923608b05d3b4883ceabd917 Mon Sep 17 00:00:00 2001 From: xiaoy312 Date: Fri, 12 Jul 2024 18:37:15 -0400 Subject: [PATCH 1/2] fix: ResourceExtensions throwing ArgumentException on windows (cherry picked from commit e0dfd7a49713d37ad99821c1630ec70547049cab) --- .../Behaviors/ResourceExtensions.cs | 4 ++ .../ResourceDictionaryExtensions.cs | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs diff --git a/src/Uno.Toolkit.UI/Behaviors/ResourceExtensions.cs b/src/Uno.Toolkit.UI/Behaviors/ResourceExtensions.cs index c22fa75c4..3f7f1dfbc 100644 --- a/src/Uno.Toolkit.UI/Behaviors/ResourceExtensions.cs +++ b/src/Uno.Toolkit.UI/Behaviors/ResourceExtensions.cs @@ -36,7 +36,11 @@ private static void OnResourcesChanged(DependencyObject d, DependencyPropertyCha { if (d is Control control && e.NewValue is ResourceDictionary newResources) { +#if !HAS_UNO + control.Resources = newResources.DeepClone(); +#else control.Resources = newResources; +#endif } } } diff --git a/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs b/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs new file mode 100644 index 000000000..589d497f2 --- /dev/null +++ b/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Uno.Extensions; +using Uno.Logging; + +#if IS_WINUI +using Microsoft.UI.Xaml; +#else +using Windows.UI.Xaml; +#endif + +namespace Uno.Toolkit.UI; + +internal static class ResourceDictionaryExtensions +{ + private static readonly ILogger _logger = typeof(ResourceDictionaryExtensions).Log(); + + /// + /// Creates a deep clone of the given . + /// + /// The resource dictionary to clone. + /// A deep clone of the resource dictionary. + /// Only the resource dictionary, and its nesting theme and merged dictionaries are deep cloned. Not their values. + public static ResourceDictionary DeepClone(this ResourceDictionary rd) + { + try + { + if (rd.Source is not null) return new ResourceDictionary() { Source = rd.Source }; + + var result = new ResourceDictionary(); + + if (rd.ThemeDictionaries is { }) + { + foreach (var (key, value) in rd.ThemeDictionaries) + { + result.ThemeDictionaries[key] = (value as ResourceDictionary)?.DeepClone() ?? value; + } + } + if (rd.MergedDictionaries is { }) + { + foreach (var md in rd.MergedDictionaries) + { + result.MergedDictionaries.Add(md.DeepClone()); + } + } + foreach (var (key, value) in rd) + { + result[key] = value; + } + + return result; + } + catch (Exception e) + { + _logger.Error("Failed to clone the resource-dictionary", e); + throw; + } + } +} From d55ec2bfc8f53cd42d86f2462ed85f02f38c599b Mon Sep 17 00:00:00 2001 From: Xiaotian Gu Date: Fri, 12 Jul 2024 20:25:38 -0400 Subject: [PATCH 2/2] chore: reviewed changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Agnès ZITTE <16295702+agneszitte@users.noreply.github.com> (cherry picked from commit 17f2e6c16ca99e618289644a7831d5ce1a2a807c) --- src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs b/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs index 589d497f2..1555bb100 100644 --- a/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs +++ b/src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs @@ -24,7 +24,7 @@ internal static class ResourceDictionaryExtensions /// /// The resource dictionary to clone. /// A deep clone of the resource dictionary. - /// Only the resource dictionary, and its nesting theme and merged dictionaries are deep cloned. Not their values. + /// Only the resource dictionary, its nesting theme, and merged dictionaries are deep-cloned. Not their values. public static ResourceDictionary DeepClone(this ResourceDictionary rd) { try