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 18c60220..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)); - } - /// /// 获取皮肤 /// @@ -82,9 +72,14 @@ namespace HandyControl.Tools /// /// get HandyControl theme /// - public static ResourceDictionary GetTheme() => new() + public static ResourceDictionary GetTheme() => _theme ??= GetStandaloneTheme(); + + public static ResourceDictionary GetStandaloneTheme() { - Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") - }; + return new() + { + Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") + }; + } } }