From abf4bf02547b7eebc42ea6c133a3ceea9bfa55b3 Mon Sep 17 00:00:00 2001 From: doudou0720 <98651603+doudou0720@users.noreply.github.com> Date: Fri, 20 Feb 2026 14:22:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(MW=5FAutoTheme):=20=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=B5=84=E6=BA=90=E5=AD=97=E5=85=B8=E4=BB=A5?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=AF=E5=8A=A8=E6=80=A7=E8=83=BD=20(#375)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(MW_AutoTheme): 异步加载资源字典以优化启动性能、 Signed-off-by: doudou0720 <98651603+doudou0720@users.noreply.github.com> * fix(MW_AutoTheme): 异步加载dark主题 Signed-off-by: doudou0720 <98651603+doudou0720@users.noreply.github.com> --------- Signed-off-by: doudou0720 <98651603+doudou0720@users.noreply.github.com> --- Ink Canvas/MainWindow_cs/MW_AutoTheme.cs | 76 +++++++++++++++++------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/Ink Canvas/MainWindow_cs/MW_AutoTheme.cs b/Ink Canvas/MainWindow_cs/MW_AutoTheme.cs index 587004bc..097eb8c8 100644 --- a/Ink Canvas/MainWindow_cs/MW_AutoTheme.cs +++ b/Ink Canvas/MainWindow_cs/MW_AutoTheme.cs @@ -1,10 +1,12 @@ -using iNKORE.UI.WPF.Modern; +using iNKORE.UI.WPF.Modern; using Microsoft.Win32; using System; using System.Collections.Generic; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; +using System.Windows.Threading; using Application = System.Windows.Application; using ui = iNKORE.UI.WPF.Modern.Controls; @@ -35,22 +37,38 @@ namespace Ink_Canvas if (theme == "Light") { + // 先加载主题 var rd1 = new ResourceDictionary - { Source = new Uri("Resources/Styles/Light.xaml", UriKind.Relative) }; + { + Source = new Uri("Resources/Styles/Light.xaml", UriKind.Relative) + }; Application.Current.Resources.MergedDictionaries.Add(rd1); - // 在主题资源之后添加其他资源 - var rd2 = new ResourceDictionary - { Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) }; - Application.Current.Resources.MergedDictionaries.Add(rd2); + // 异步加载图形资源,避免阻塞启动 + _ = Task.Run(async () => + { + await Task.Delay(100); + Dispatcher.Invoke(() => + { + var rd2 = new ResourceDictionary + { + Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) + }; + Application.Current.Resources.MergedDictionaries.Add(rd2); - var rd3 = new ResourceDictionary - { Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) }; - Application.Current.Resources.MergedDictionaries.Add(rd3); + var rd3 = new ResourceDictionary + { + Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) + }; + Application.Current.Resources.MergedDictionaries.Add(rd3); - var rd4 = new ResourceDictionary - { Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) }; - Application.Current.Resources.MergedDictionaries.Add(rd4); + var rd4 = new ResourceDictionary + { + Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) + }; + Application.Current.Resources.MergedDictionaries.Add(rd4); + }); + }); ThemeManager.SetRequestedTheme(window, ElementTheme.Light); @@ -83,21 +101,35 @@ namespace Ink_Canvas } else if (theme == "Dark") { + // 先加载主题 var rd1 = new ResourceDictionary { Source = new Uri("Resources/Styles/Dark.xaml", UriKind.Relative) }; Application.Current.Resources.MergedDictionaries.Add(rd1); - // 在主题资源之后添加其他资源 - var rd2 = new ResourceDictionary - { Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) }; - Application.Current.Resources.MergedDictionaries.Add(rd2); + // 异步加载图形资源,避免阻塞启动 + _ = Task.Run(async () => + { + await Task.Delay(100); + Dispatcher.Invoke(() => + { + var rd2 = new ResourceDictionary + { + Source = new Uri("Resources/DrawShapeImageDictionary.xaml", UriKind.Relative) + }; + Application.Current.Resources.MergedDictionaries.Add(rd2); - var rd3 = new ResourceDictionary - { Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) }; - Application.Current.Resources.MergedDictionaries.Add(rd3); + var rd3 = new ResourceDictionary + { + Source = new Uri("Resources/SeewoImageDictionary.xaml", UriKind.Relative) + }; + Application.Current.Resources.MergedDictionaries.Add(rd3); - var rd4 = new ResourceDictionary - { Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) }; - Application.Current.Resources.MergedDictionaries.Add(rd4); + var rd4 = new ResourceDictionary + { + Source = new Uri("Resources/IconImageDictionary.xaml", UriKind.Relative) + }; + Application.Current.Resources.MergedDictionaries.Add(rd4); + }); + }); ThemeManager.SetRequestedTheme(window, ElementTheme.Dark);