-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1190 from unoplatform/dev/xygu/20240712/resource-…
…ext--win-throws fix: ResourceExtensions throwing ArgumentException on windows
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Uno.Toolkit.UI/Extensions/ResourceDictionaryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
/// <summary> | ||
/// Creates a deep clone of the given <see cref="ResourceDictionary"/>. | ||
/// </summary> | ||
/// <param name="rd">The resource dictionary to clone.</param> | ||
/// <returns>A deep clone of the resource dictionary.</returns> | ||
/// <remarks>Only the resource dictionary, its nesting theme, and merged dictionaries are deep-cloned. Not their values.</remarks> | ||
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; | ||
} | ||
} | ||
} |