From 2607bad43fc56e21067f7376852a880fdbc344dc Mon Sep 17 00:00:00 2001 From: laggage <1634205628@qq.com> Date: Thu, 30 Jan 2020 18:05:02 +0800 Subject: [PATCH] =?UTF-8?q?UI=E5=AE=8C=E5=96=84=E5=92=8C=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WinPowerHelper.Wpf/App.xaml | 5 +- WinPowerHelper.Wpf/MainWindow.xaml | 29 ++-- .../Styles/Controls.ComboBox.xaml | 77 ++++++----- .../Styles/Themes/Light.Blue.xaml | 2 +- .../Styles/Themes/Light.Pink.xaml | 2 +- WinPowerHelper.Wpf/ThemeManager/Theme.cs | 2 +- .../ThemeManager/ThemeManager.cs | 53 ++++++- .../ViewModels/MainViewModel.cs | 20 +++ WinPowerHelper.Wpf/Views/WindowTitleBar.xaml | 129 ++++++++++++++++++ .../Views/WindowTitleBar.xaml.cs | 36 +++++ WinPowerHelper.Wpf/Views/cb89p-4nv71-003.ico | Bin 0 -> 16958 bytes WinPowerHelper.Wpf/WinPowerHelper.Wpf.csproj | 2 + 12 files changed, 301 insertions(+), 56 deletions(-) create mode 100644 WinPowerHelper.Wpf/Views/WindowTitleBar.xaml create mode 100644 WinPowerHelper.Wpf/Views/WindowTitleBar.xaml.cs create mode 100644 WinPowerHelper.Wpf/Views/cb89p-4nv71-003.ico diff --git a/WinPowerHelper.Wpf/App.xaml b/WinPowerHelper.Wpf/App.xaml index cdcf5c0..7981f7e 100644 --- a/WinPowerHelper.Wpf/App.xaml +++ b/WinPowerHelper.Wpf/App.xaml @@ -2,7 +2,8 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewmodels="clr-namespace:WinPowerHelper.Wpf.ViewModels" - StartupUri="MainWindow.xaml"> + StartupUri="MainWindow.xaml" + xmlns:system="clr-namespace:System;assembly=System.Runtime"> @@ -11,6 +12,8 @@ + 32 + 14 diff --git a/WinPowerHelper.Wpf/MainWindow.xaml b/WinPowerHelper.Wpf/MainWindow.xaml index 2c4ce58..b8d75b9 100644 --- a/WinPowerHelper.Wpf/MainWindow.xaml +++ b/WinPowerHelper.Wpf/MainWindow.xaml @@ -8,26 +8,38 @@ xmlns:extensions="clr-namespace:WinPowerHelper.Wpf.Extensions" mc:Ignorable="d" DataContext="{StaticResource MainViewModel}" - Title="{Binding AppTitle}" - MinHeight="240" MinWidth="420" Width="500" Height="250" - MaxHeight="400" MaxWidth="680" ResizeMode="CanMinimize"> + Title="{Binding AppTitle}" WindowStyle="None" + MinHeight="240" MinWidth="420" Width="500" Height="200" + BorderThickness="1" + MaxHeight="400" MaxWidth="680" ResizeMode="CanMinimize" BorderBrush="{DynamicResource Brushes.Accent}"> + - - + + + - + + + + + - - \ No newline at end of file diff --git a/WinPowerHelper.Wpf/Styles/Themes/Light.Blue.xaml b/WinPowerHelper.Wpf/Styles/Themes/Light.Blue.xaml index 1b385ff..223315c 100644 --- a/WinPowerHelper.Wpf/Styles/Themes/Light.Blue.xaml +++ b/WinPowerHelper.Wpf/Styles/Themes/Light.Blue.xaml @@ -5,7 +5,7 @@ xmlns:system="clr-namespace:System;assembly=System.Runtime" mc:Ignorable="options"> Light.Blue - Blue (Light) + Blue (Light) Light Blue diff --git a/WinPowerHelper.Wpf/Styles/Themes/Light.Pink.xaml b/WinPowerHelper.Wpf/Styles/Themes/Light.Pink.xaml index 9c80db1..2a23309 100644 --- a/WinPowerHelper.Wpf/Styles/Themes/Light.Pink.xaml +++ b/WinPowerHelper.Wpf/Styles/Themes/Light.Pink.xaml @@ -6,7 +6,7 @@ mc:Ignorable="options"> Light.Pink - Pink (Light) + Pink (Light) Light Pink diff --git a/WinPowerHelper.Wpf/ThemeManager/Theme.cs b/WinPowerHelper.Wpf/ThemeManager/Theme.cs index 70e39f9..1d6595d 100644 --- a/WinPowerHelper.Wpf/ThemeManager/Theme.cs +++ b/WinPowerHelper.Wpf/ThemeManager/Theme.cs @@ -21,7 +21,7 @@ public Theme(ResourceDictionary resources) public const string ThemeNameKey = "Theme.Name"; public const string ThemeDisplayNameKey = "Theme.DisplayName"; public const string ThemeBaseColorSchemeKey = "Theme.BaseColorScheme"; - public const string ThemeColorSchemeKey = "Theme.ColorSchema"; + public const string ThemeColorSchemeKey = "Theme.ColorScheme"; public const string ThemeShowcaseBrushKey = "Theme.ShowcaseBrush"; public string Name { get; set; } diff --git a/WinPowerHelper.Wpf/ThemeManager/ThemeManager.cs b/WinPowerHelper.Wpf/ThemeManager/ThemeManager.cs index d8d4006..61bf9ad 100644 --- a/WinPowerHelper.Wpf/ThemeManager/ThemeManager.cs +++ b/WinPowerHelper.Wpf/ThemeManager/ThemeManager.cs @@ -3,8 +3,8 @@ using System; using System.Collections; using System.Collections.Generic; + using System.Linq; using System.Resources; - using System.Text; using System.Windows; public static class ThemeManager @@ -28,11 +28,18 @@ private set } } + /// + /// 确保 存在数据 + /// + /// + /// 这个方法中不能调用属性 , 否则会造成 "循环引用" + /// 这个方法会被一直递归, 直到堆栈溢出 + /// private static void EnsureThemes() { - if (Themes.Count > 0) return; + if (_themes.Count > 0) return; var assembly = typeof(ThemeManager).Assembly; - var assemblyName = assembly.GetName(); + var assemblyName = assembly.GetName().Name; var resourceNames = assembly.GetManifestResourceNames(); foreach (var resourceName in resourceNames) @@ -44,13 +51,47 @@ private static void EnsureThemes() { foreach (DictionaryEntry entry in reader) { - throw new NotImplementedException(); + string stringKey = entry.Key as string; + if (stringKey == null || + !stringKey.Contains("/themes/", StringComparison.OrdinalIgnoreCase) || + !stringKey.EndsWith(".baml", StringComparison.OrdinalIgnoreCase) || + stringKey.EndsWith("generic.baml", StringComparison.OrdinalIgnoreCase)) continue; + + var resourceDictionary = new ResourceDictionary() + { + Source = new Uri( + $"pack://application:,,,/{assemblyName};component/{stringKey.Replace("baml", "xaml")}") + }; + + if (resourceDictionary != null && + resourceDictionary.MergedDictionaries.Count == 0) + _themes.Add(new Theme(resourceDictionary)); + } } - - } } + public static void ChangeThemeColorScheme(ResourceDictionary resources, string newColorScheme) + { + var oldThemeResource = resources.MergedDictionaries.FirstOrDefault( + x => x.Source.LocalPath.Contains( + "light.", StringComparison.OrdinalIgnoreCase)); + + if (oldThemeResource[Theme.ThemeColorSchemeKey] as string == newColorScheme) return; + + var newTheme = Themes.FirstOrDefault(x => + x.ColorScheme.Equals(newColorScheme, StringComparison.OrdinalIgnoreCase)); + + string assemblyName = typeof(ThemeManager).Assembly.GetName().Name; + var newThemeResource = new ResourceDictionary + { + Source = new Uri( + $"pack://application:,,,/{assemblyName};component/Styles/Themes/Light.{newColorScheme}.xaml") + }; + resources.MergedDictionaries.Add(newThemeResource); + if (oldThemeResource != null) + resources.MergedDictionaries.Remove(oldThemeResource); + } } } diff --git a/WinPowerHelper.Wpf/ViewModels/MainViewModel.cs b/WinPowerHelper.Wpf/ViewModels/MainViewModel.cs index 8663976..8636e6c 100644 --- a/WinPowerHelper.Wpf/ViewModels/MainViewModel.cs +++ b/WinPowerHelper.Wpf/ViewModels/MainViewModel.cs @@ -9,6 +9,9 @@ using WinPowerHelper.Core.Models; using WinPowerHelper.Core.Services; using WinPowerHelper.Wpf.Views; + using ThemeManager; + using System.Windows; + using System.Linq; internal class MainViewModel : BindableBase { @@ -24,6 +27,11 @@ public MainViewModel() Core.Models.PowerOptions.Shutdown, Core.Models.PowerOptions.Restart, }; + Themes = ThemeManager.Themes; + SelectedTheme = + Themes.FirstOrDefault( + x => x.ColorScheme.Contains( + "blue", StringComparison.OrdinalIgnoreCase)); } public static string AppTitle => "Windows定时关机助手"; @@ -35,9 +43,21 @@ public TimeSpan Interval set => SetProperty(ref _interval, value); } + public IList Themes { get; set; } public List PowerOptions { get; set; } public PowerOptions SelectedPowerOption { get; set; } + private Theme _selectedTheme; + public Theme SelectedTheme + { + get => _selectedTheme; + set + { + SetProperty(ref _selectedTheme, value); + ThemeManager.ChangeThemeColorScheme(Application.Current.Resources, value.ColorScheme); + } + } + public ICommand SetIntervalCmd { get; private set; } public ICommand SetIntervalToZeroCmd { get; private set; } public ICommand BeginOrStopTimingCmd { get; private set; } diff --git a/WinPowerHelper.Wpf/Views/WindowTitleBar.xaml b/WinPowerHelper.Wpf/Views/WindowTitleBar.xaml new file mode 100644 index 0000000..fad78d2 --- /dev/null +++ b/WinPowerHelper.Wpf/Views/WindowTitleBar.xaml @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/WinPowerHelper.Wpf/Views/WindowTitleBar.xaml.cs b/WinPowerHelper.Wpf/Views/WindowTitleBar.xaml.cs new file mode 100644 index 0000000..6ce311b --- /dev/null +++ b/WinPowerHelper.Wpf/Views/WindowTitleBar.xaml.cs @@ -0,0 +1,36 @@ +namespace WinPowerHelper.Wpf.Views +{ + using System.Windows.Controls; + using System.Windows.Input; + using System.Windows; + + /// + /// Interaction logic for WindowTitleBar.xaml + /// + public partial class WindowTitleBar : UserControl + { + public WindowTitleBar() + { + InitializeComponent(); + + } + + protected override void OnMouseMove(MouseEventArgs e) + { + base.OnMouseMove(e); + if (e.LeftButton == MouseButtonState.Pressed) + Application.Current.MainWindow.DragMove(); + } + + + private void Minimize_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) + { + Application.Current.MainWindow.WindowState = WindowState.Minimized; + } + + private void Close_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) + { + Application.Current.MainWindow.Close(); + } + } +} diff --git a/WinPowerHelper.Wpf/Views/cb89p-4nv71-003.ico b/WinPowerHelper.Wpf/Views/cb89p-4nv71-003.ico new file mode 100644 index 0000000000000000000000000000000000000000..8dabfba645cbffb54ca3627e90e532efd6420e05 GIT binary patch literal 16958 zcmeI1Ux-vy9LKM_(O8kTDi!LQx}i&n`(}vXuB#7*5SVYNlu54Alf6i>?e0rdltvi# zheX^WF+zOnAO3ksQA!FyeQ<5p2r-GK2q9X$eLk~0?K140KfBkNyLi9LXU^0>j5AGys|33H^et`Kf+0}>vrkoGMT_uds{$2>74;Dfx zHE_eDun6uhb#s;LwPz!|F(ABjD!c+SAxYAnH{lmJ4o^U_XH<#(#>Tnu z9@zdj;9N_Rw5J1pgzL}=#pZM+_TNui&aLm^8oUchjF*owuD9SfI0YLZ17k`1%ZYzJ z`^Vuh=-02H9qxf7YAu604L8C0x&+3O_V0jF`=7CN8~EG@;AM#NDOELB6C>z?8*mP` zg0_yOa`K<$eDDbT1&3fIM72j%&E>OM24BKicnKylUg!GP;M(5;#)7148Ur+5?SwTj z5&IYO`4_>pvIuISIy#*Wb6_@TpVx{>oAVUAubjGa+c74(FSs|F9~9evsV-aE`z(9` zpTj1Y1xbBgAzk>)PH9XP_TG1>0W=4Nyz@Y?@##n5(-M`r&nOzQ!T_Z2Qiw zU9b}DH)-_egW3s2HRT?pw|g&(h|=Sn}LrJzN6!x830S zs)Hm+d)yb?8@2riv_h1;e?Nq90Tw`%^x+=33g&y8U>YPz+OrG#VF1h%joDH1{$9}j zK3E7*a&5H3F>oDx2D2ea(jN1JGjIT!p$?){iT$=?o@M;|4h};S?KhTfhgIPBg<`72 ze%tYWW9w#k4yHrWWS;1_oUe|zn5;ht&i~6$Z2nW7lkwGcZy&|xR_^A6zi7Yt`VBY% z^WaXB_OAo?zz!JC`8*$+)Y1oeCbc2y;8PEz9+ + @@ -29,6 +30,7 @@ + \ No newline at end of file