From d693312338b833b1ae99578d8021d479a8921120 Mon Sep 17 00:00:00 2001 From: cuiliang Date: Wed, 11 Aug 2021 14:38:45 +0800 Subject: [PATCH 1/2] Avoid duplicated ResourceDictionary --- .../Tools/Helper/ResourceHelper.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs b/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs index 18c60220..9bdb4b87 100644 --- a/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs +++ b/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs @@ -79,12 +79,24 @@ namespace HandyControl.Tools Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin}.xaml") }; + + + private static ResourceDictionary _theme; + /// /// get HandyControl theme /// - public static ResourceDictionary GetTheme() => new() + public static ResourceDictionary GetTheme() { - Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") - }; + if (_theme == null) + { + _theme = new() + { + Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") + }; + } + + return _theme; + } } } From 8960758688ced1261c2701f00d6097b6b3379678 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Mon, 23 Aug 2021 23:29:17 +0800 Subject: [PATCH 2/2] add GetStandaloneTheme method --- .../HandyControl_Shared/Themes/Theme.cs | 35 +++++++++++-------- .../Tools/Helper/ResourceHelper.cs | 35 +++++-------------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/Shared/HandyControl_Shared/Themes/Theme.cs b/src/Shared/HandyControl_Shared/Themes/Theme.cs index 26742ac6..a4ececf0 100644 --- a/src/Shared/HandyControl_Shared/Themes/Theme.cs +++ b/src/Shared/HandyControl_Shared/Themes/Theme.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Windows; using System.Windows.Media; using HandyControl.Data; @@ -204,23 +205,17 @@ namespace HandyControl.Themes return _precSkin; } - private void UpdateAccentColor(Color color) + public static Theme GetTheme(string name, ResourceDictionary resourceDictionary) { - _precSkin[ResourceToken.PrimaryColor] = color; - _precSkin[ResourceToken.DarkPrimaryColor] = color; - _precSkin[ResourceToken.TitleColor] = color; - _precSkin[ResourceToken.SecondaryTitleColor] = color; + if (string.IsNullOrEmpty(name) || resourceDictionary == null) + { + return null; + } + + return resourceDictionary.MergedDictionaries.OfType().FirstOrDefault(item => Equals(item.Name, name)); } - private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin); - - private ResourceDictionary _theme; - - public virtual ResourceDictionary GetTheme() - { - _theme ??= ResourceHelper.GetTheme(); - return _theme; - } + public virtual ResourceDictionary GetTheme() => ResourceHelper.GetTheme(); private void InitResource() { @@ -233,10 +228,20 @@ namespace HandyControl.Themes MergedDictionaries.Add(GetSkin(Skin)); MergedDictionaries.Add(GetTheme()); } + + private void UpdateAccentColor(Color color) + { + _precSkin[ResourceToken.PrimaryColor] = color; + _precSkin[ResourceToken.DarkPrimaryColor] = color; + _precSkin[ResourceToken.TitleColor] = color; + _precSkin[ResourceToken.SecondaryTitleColor] = color; + } + + private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin); } public class StandaloneTheme : Theme { - public override ResourceDictionary GetTheme() => ResourceHelper.GetTheme(); + public override ResourceDictionary GetTheme() => ResourceHelper.GetStandaloneTheme(); } } diff --git a/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs b/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs index 9bdb4b87..be63404b 100644 --- a/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs +++ b/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs @@ -1,9 +1,7 @@ using System; -using System.Linq; using System.Reflection; using System.Windows; using HandyControl.Data; -using HandyControl.Themes; namespace HandyControl.Tools { @@ -12,6 +10,8 @@ namespace HandyControl.Tools /// public class ResourceHelper { + private static ResourceDictionary _theme; + /// /// 获取资源 /// @@ -37,16 +37,6 @@ namespace HandyControl.Tools return default; } - public static Theme GetTheme(string name, ResourceDictionary resourceDictionary) - { - if (string.IsNullOrEmpty(name) || resourceDictionary == null) - { - return null; - } - - return resourceDictionary.MergedDictionaries.OfType().FirstOrDefault(item => Equals(item.Name, name)); - } - /// /// 获取皮肤 /// @@ -79,24 +69,17 @@ namespace HandyControl.Tools Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin}.xaml") }; - - - private static ResourceDictionary _theme; - /// /// get HandyControl theme /// - public static ResourceDictionary GetTheme() - { - if (_theme == null) - { - _theme = new() - { - Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") - }; - } + public static ResourceDictionary GetTheme() => _theme ??= GetStandaloneTheme(); - return _theme; + public static ResourceDictionary GetStandaloneTheme() + { + return new() + { + Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") + }; } } }