From 8f1bb427147436c9bb7ccf2e76b24fee9fdccade Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sun, 19 Apr 2020 14:58:48 +0800 Subject: [PATCH] close #361 --- src/Shared/HandyControlDemo_Shared/App.xaml | 15 ++----- .../HandyControlDemo_Shared/App.xaml.cs | 18 ++------ .../HandyControlDemo_Shared.projitems | 1 + .../Resources/DemoTheme.cs | 16 +++++++ .../HandyControl_Shared.projitems | 1 + .../Properties/CoreAssemblyInfo.cs | 1 + .../Properties/NetAssemblyInfo.cs | 1 + .../HandyControl_Shared/Themes/Theme.cs | 45 +++++++++++++++++++ .../Tools/Helper/ResourceHelper.cs | 8 ++++ 9 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 src/Shared/HandyControlDemo_Shared/Resources/DemoTheme.cs create mode 100644 src/Shared/HandyControl_Shared/Themes/Theme.cs diff --git a/src/Shared/HandyControlDemo_Shared/App.xaml b/src/Shared/HandyControlDemo_Shared/App.xaml index 2c7bdbe8..26d9448c 100644 --- a/src/Shared/HandyControlDemo_Shared/App.xaml +++ b/src/Shared/HandyControlDemo_Shared/App.xaml @@ -8,24 +8,15 @@ xmlns:data="clr-namespace:HandyControlDemo.Data" xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs" + xmlns:resources="clr-namespace:HandyControlDemo.Resources" d1P1:Ignorable="d" StartupUri="MainWindow.xaml"> - - - - - - - - - - - - + + diff --git a/src/Shared/HandyControlDemo_Shared/App.xaml.cs b/src/Shared/HandyControlDemo_Shared/App.xaml.cs index 1eb7fa4f..bbc6ac4f 100644 --- a/src/Shared/HandyControlDemo_Shared/App.xaml.cs +++ b/src/Shared/HandyControlDemo_Shared/App.xaml.cs @@ -3,7 +3,9 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net; +#if !netle40 using System.Runtime; +#endif using System.Security.Authentication; using System.Threading; using System.Windows; @@ -86,21 +88,9 @@ namespace HandyControlDemo internal void UpdateSkin(SkinType skin) { - var skins0 = Resources.MergedDictionaries[0]; - skins0.MergedDictionaries.Clear(); - skins0.MergedDictionaries.Add(ResourceHelper.GetSkin(skin)); - skins0.MergedDictionaries.Add(ResourceHelper.GetSkin(typeof(App).Assembly, "Resources/Themes", skin)); + ResourceHelper.GetTheme("HandyTheme", Resources).Skin = skin; + ResourceHelper.GetTheme("DemoTheme", Resources).Skin = skin; - var skins1 = Resources.MergedDictionaries[1]; - skins1.MergedDictionaries.Clear(); - skins1.MergedDictionaries.Add(new ResourceDictionary - { - Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml") - }); - skins1.MergedDictionaries.Add(new ResourceDictionary - { - Source = new Uri("pack://application:,,,/HandyControlDemo;component/Resources/Themes/Theme.xaml") - }); Current.MainWindow?.OnApplyTemplate(); } diff --git a/src/Shared/HandyControlDemo_Shared/HandyControlDemo_Shared.projitems b/src/Shared/HandyControlDemo_Shared/HandyControlDemo_Shared.projitems index 60ba1d0b..2dbff989 100644 --- a/src/Shared/HandyControlDemo_Shared/HandyControlDemo_Shared.projitems +++ b/src/Shared/HandyControlDemo_Shared/HandyControlDemo_Shared.projitems @@ -29,6 +29,7 @@ Code MainWindow.xaml + diff --git a/src/Shared/HandyControlDemo_Shared/Resources/DemoTheme.cs b/src/Shared/HandyControlDemo_Shared/Resources/DemoTheme.cs new file mode 100644 index 00000000..fdfba9ad --- /dev/null +++ b/src/Shared/HandyControlDemo_Shared/Resources/DemoTheme.cs @@ -0,0 +1,16 @@ +using System; +using System.Windows; +using HandyControl.Data; +using HandyControl.Themes; +using HandyControl.Tools; + +namespace HandyControlDemo.Resources +{ + public class DemoTheme : Theme + { + public override Uri ThemeUri => new Uri("pack://application:,,,/HandyControlDemo;component/Resources/Themes/Theme.xaml"); + + public override ResourceDictionary GetSkin(SkinType skinType) => + ResourceHelper.GetSkin(typeof(App).Assembly, "Resources/Themes", skinType); + } +} diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index 7fd0cd13..df9c1a9b 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -84,6 +84,7 @@ + diff --git a/src/Shared/HandyControl_Shared/Properties/CoreAssemblyInfo.cs b/src/Shared/HandyControl_Shared/Properties/CoreAssemblyInfo.cs index 396c40a5..711938bb 100644 --- a/src/Shared/HandyControl_Shared/Properties/CoreAssemblyInfo.cs +++ b/src/Shared/HandyControl_Shared/Properties/CoreAssemblyInfo.cs @@ -10,4 +10,5 @@ [assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Media.Effects")] [assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Data")] [assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Properties.Langs")] +[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Themes")] [assembly: XmlnsPrefix("https://handyorg.github.io/handycontrol", "hc")] \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Properties/NetAssemblyInfo.cs b/src/Shared/HandyControl_Shared/Properties/NetAssemblyInfo.cs index ed816c73..911fdcb8 100644 --- a/src/Shared/HandyControl_Shared/Properties/NetAssemblyInfo.cs +++ b/src/Shared/HandyControl_Shared/Properties/NetAssemblyInfo.cs @@ -23,4 +23,5 @@ using System.Runtime.InteropServices; [assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Media.Effects")] [assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Data")] [assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Properties.Langs")] +[assembly: XmlnsDefinition("https://handyorg.github.io/handycontrol", "HandyControl.Themes")] [assembly: XmlnsPrefix("https://handyorg.github.io/handycontrol", "hc")] \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Themes/Theme.cs b/src/Shared/HandyControl_Shared/Themes/Theme.cs new file mode 100644 index 00000000..25ac51e5 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Themes/Theme.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.ObjectModel; +using System.Windows; +using HandyControl.Data; +using HandyControl.Tools; + +namespace HandyControl.Themes +{ + public class Theme : ResourceDictionary + { + public Theme() => UpdateResource(); + + private SkinType _skin; + + public virtual SkinType Skin + { + get => _skin; + set + { + if (_skin == value) return; + _skin = value; + + UpdateResource(); + } + } + + public string Name { get; set; } + + public virtual Uri ThemeUri => new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml"); + + public virtual ResourceDictionary GetSkin(SkinType skinType) => ResourceHelper.GetSkin(skinType); + + private void UpdateResource() + { + base.MergedDictionaries.Clear(); + base.MergedDictionaries.Add(GetSkin(Skin)); + base.MergedDictionaries.Add(new ResourceDictionary + { + Source = ThemeUri + }); + } + + public new Collection MergedDictionaries => throw new NotSupportedException(); + } +} diff --git a/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs b/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs index a7518c02..d4462531 100644 --- a/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs +++ b/src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Reflection; using System.Windows; using HandyControl.Data; +using HandyControl.Themes; namespace HandyControl.Tools { @@ -85,5 +86,12 @@ namespace HandyControl.Tools }; } } + + 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)); + } } } \ No newline at end of file