improve:启动速度

This commit is contained in:
2026-04-30 15:04:54 +08:00
parent 7d52573595
commit c68596b91e
3 changed files with 95 additions and 69 deletions
+68 -30
View File
@@ -85,6 +85,7 @@ namespace Ink_Canvas
// 新增:启动画面相关 // 新增:启动画面相关
private static SplashScreen _splashScreen; private static SplashScreen _splashScreen;
private static bool _isSplashScreenShown = false; private static bool _isSplashScreenShown = false;
private static System.Resources.ResourceSet _pendingLocalizedResourceSet;
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int SetCurrentProcessExplicitAppUserModelID(string appId); private static extern int SetCurrentProcessExplicitAppUserModelID(string appId);
@@ -763,20 +764,14 @@ namespace Ink_Canvas
appStartTime = DateTime.Now; appStartTime = DateTime.Now;
appStartupStartTime = DateTime.Now; appStartupStartTime = DateTime.Now;
var resourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); TryApplyPreferredLanguageFromSettings();
if (resourceSet != null)
{ _pendingLocalizedResourceSet = Strings.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (System.Collections.DictionaryEntry entry in resourceSet)
{
if (entry.Key is string key && entry.Value is string value)
Current.Resources[key] = value;
}
}
// 根据设置决定是否显示启动画面 // 根据设置决定是否显示启动画面
if (ShouldShowSplashScreen() && !IsLaunchByFileOrUri(e.Args)) if (ShouldShowSplashScreen() && !IsLaunchByFileOrUri(e.Args))
{ {
await Task.Delay(200); await Task.Delay(100);
ShowSplashScreen(); ShowSplashScreen();
SetSplashMessage(Strings.GetString("Splash_Starting")); SetSplashMessage(Strings.GetString("Splash_Starting"));
SetSplashProgress(20); SetSplashProgress(20);
@@ -785,7 +780,7 @@ namespace Ink_Canvas
Application.Current.Dispatcher.Invoke(() => { }, DispatcherPriority.Render); Application.Current.Dispatcher.Invoke(() => { }, DispatcherPriority.Render);
} }
await Task.Delay(200); await Task.Delay(100);
RootPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; RootPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
LogHelper.NewLog(string.Format("Ink Canvas Starting (Version: {0})", Assembly.GetExecutingAssembly().GetName().Version)); LogHelper.NewLog(string.Format("Ink Canvas Starting (Version: {0})", Assembly.GetExecutingAssembly().GetName().Version));
@@ -821,15 +816,6 @@ namespace Ink_Canvas
{ {
SetSplashMessage("正在初始化组件..."); SetSplashMessage("正在初始化组件...");
SetSplashProgress(40); SetSplashProgress(40);
await Task.Delay(200);
}
try
{
IACoreDllExtractor.ExtractIACoreDlls();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放IACore DLL时出错: {ex.Message}", LogHelper.LogType.Error);
} }
// 释放UIAccess DLL // 释放UIAccess DLL
@@ -838,21 +824,13 @@ namespace Ink_Canvas
SetSplashMessage("正在初始化组件..."); SetSplashMessage("正在初始化组件...");
SetSplashProgress(50); SetSplashProgress(50);
} }
try
{
UIAccessDllExtractor.ExtractUIAccessDlls();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放UIAccess DLL时出错: {ex.Message}", LogHelper.LogType.Error);
}
// 记录应用启动(设备标识符) // 记录应用启动(设备标识符)
if (_isSplashScreenShown) if (_isSplashScreenShown)
{ {
SetSplashMessage("正在加载配置..."); SetSplashMessage("正在加载配置...");
SetSplashProgress(60); SetSplashProgress(60);
await Task.Delay(200); await Task.Delay(100);
} }
DeviceIdentifier.RecordAppLaunch(); DeviceIdentifier.RecordAppLaunch();
try try
@@ -1137,7 +1115,6 @@ namespace Ink_Canvas
} }
_taskbar = (TaskbarIcon)FindResource("TaskbarTrayIcon"); _taskbar = (TaskbarIcon)FindResource("TaskbarTrayIcon");
_taskbar.ForceCreate();
StartArgs = e.Args; StartArgs = e.Args;
@@ -1209,6 +1186,19 @@ namespace Ink_Canvas
}; };
mainWindow.Show(); mainWindow.Show();
_ = Task.Run(async () =>
{
await Task.Delay(600);
Dispatcher.Invoke(() => _taskbar?.ForceCreate());
});
Dispatcher.BeginInvoke(new Action(() =>
{
if (_pendingLocalizedResourceSet != null)
{
LoadLocalizedResources(_pendingLocalizedResourceSet);
_pendingLocalizedResourceSet = null;
}
}), DispatcherPriority.ApplicationIdle);
// 处理启动时的URI参数 // 处理启动时的URI参数
string startupUriArg = e.Args.FirstOrDefault(a => a.StartsWith("icc:", StringComparison.OrdinalIgnoreCase)); string startupUriArg = e.Args.FirstOrDefault(a => a.StartsWith("icc:", StringComparison.OrdinalIgnoreCase));
@@ -1235,6 +1225,24 @@ namespace Ink_Canvas
{ {
await Task.Delay(1200); await Task.Delay(1200);
try
{
IACoreDllExtractor.ExtractIACoreDlls();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放IACore DLL时出错: {ex.Message}", LogHelper.LogType.Error);
}
try
{
UIAccessDllExtractor.ExtractUIAccessDlls();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"释放UIAccess DLL时出错: {ex.Message}", LogHelper.LogType.Error);
}
try try
{ {
LogHelper.WriteLogToFile("开始注册.icstk文件关联"); LogHelper.WriteLogToFile("开始注册.icstk文件关联");
@@ -1283,6 +1291,36 @@ namespace Ink_Canvas
} }
} }
private void TryApplyPreferredLanguageFromSettings()
{
try
{
var settingsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "Settings.json");
if (!File.Exists(settingsPath)) return;
var json = File.ReadAllText(settingsPath);
dynamic obj = JsonConvert.DeserializeObject(json);
string preferredLanguage = obj?["appearance"]?["language"]?.ToString();
if (!string.IsNullOrWhiteSpace(preferredLanguage))
{
LocalizationHelper.TrySetCulture(preferredLanguage);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"启动时预加载语言失败: {ex.Message}", LogHelper.LogType.Error);
}
}
private void LoadLocalizedResources(System.Resources.ResourceSet resourceSet)
{
foreach (System.Collections.DictionaryEntry entry in resourceSet)
{
if (entry.Key is string key && entry.Value is string value)
Current.Resources[key] = value;
}
}
private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{ {
try try
+25 -37
View File
@@ -96,25 +96,6 @@ namespace Ink_Canvas
处于画板模式内:Topmost == false / currentMode != 0 处于画板模式内:Topmost == false / currentMode != 0
处于 PPT 放映内:BtnPPTSlideShowEnd.Visibility 处于 PPT 放映内:BtnPPTSlideShowEnd.Visibility
*/ */
try
{
var path = App.RootPath + settingsFileName;
if (File.Exists(path))
{
var json = File.ReadAllText(path);
var loadedSettings = JsonConvert.DeserializeObject<Settings>(json);
var preferredLanguage = loadedSettings?.Appearance?.Language;
if (!string.IsNullOrWhiteSpace(preferredLanguage))
{
LocalizationHelper.TrySetCulture(preferredLanguage);
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"启动时预加载语言失败: {ex.Message}", LogHelper.LogType.Error);
}
InitializeComponent(); InitializeComponent();
BlackboardLeftSide.Visibility = Visibility.Collapsed; BlackboardLeftSide.Visibility = Visibility.Collapsed;
@@ -337,9 +318,6 @@ namespace Ink_Canvas
Activated += Window_Activated; Activated += Window_Activated;
Deactivated += Window_Deactivated; Deactivated += Window_Deactivated;
// 为滑块控件添加触摸事件支持
AddTouchSupportToSliders();
// 初始化计时器控件事件 // 初始化计时器控件事件
Dispatcher.BeginInvoke(new Action(() => Dispatcher.BeginInvoke(new Action(() =>
{ {
@@ -1191,6 +1169,7 @@ namespace Ink_Canvas
private bool _suppressChickenSoupSourceSelectionChanged; private bool _suppressChickenSoupSourceSelectionChanged;
private bool forcePointEraser; private bool forcePointEraser;
private bool _pendingStartupAutoUpdateCheck; private bool _pendingStartupAutoUpdateCheck;
private bool _sliderTouchSupportInitialized;
/// <summary> /// <summary>
/// 在窗口加载完成后初始化应用的核心子系统、UI 状态和运行时监控组件。 /// 在窗口加载完成后初始化应用的核心子系统、UI 状态和运行时监控组件。
@@ -1237,7 +1216,10 @@ namespace Ink_Canvas
} }
//TextBlockVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString(); //TextBlockVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
LogHelper.WriteLogToFile("Ink Canvas Loaded", LogHelper.LogType.Event); Dispatcher.BeginInvoke(new Action(() =>
{
LogHelper.WriteLogToFile("Ink Canvas Loaded", LogHelper.LogType.Event);
}), DispatcherPriority.ApplicationIdle);
isLoaded = true; isLoaded = true;
EnsureRealtimeStylusPipelineBinding(); EnsureRealtimeStylusPipelineBinding();
@@ -1317,17 +1299,11 @@ namespace Ink_Canvas
_ = RunDeferredStartupPhaseBAsync(); _ = RunDeferredStartupPhaseBAsync();
// 初始化剪贴板监控
InitializeClipboardMonitoring();
// 初始化悬浮窗拦截管理器
InitializeFloatingWindowInterceptor();
// 初始化全局快捷键管理器
InitializeGlobalHotkeyManager();
// 初始化墨迹渐隐管理器 // 初始化墨迹渐隐管理器
InitializeInkFadeManager(); Dispatcher.BeginInvoke(new Action(() =>
{
InitializeInkFadeManager();
}), DispatcherPriority.ApplicationIdle);
// 处理命令行参数中的文件路径 // 处理命令行参数中的文件路径
HandleCommandLineFileOpen(); HandleCommandLineFileOpen();
@@ -1426,7 +1402,12 @@ namespace Ink_Canvas
}; };
} }
}), DispatcherPriority.Loaded); }), DispatcherPriority.Loaded);
AddTouchSupportToSliders(); Dispatcher.BeginInvoke(new Action(() =>
{
if (_sliderTouchSupportInitialized) return;
AddTouchSupportToSliders();
_sliderTouchSupportInitialized = true;
}), DispatcherPriority.ApplicationIdle);
} }
private void ApplyLanguageFromSettings() private void ApplyLanguageFromSettings()
@@ -2412,6 +2393,10 @@ namespace Ink_Canvas
LogHelper.WriteLogToFile($"[MainWindow] 初始化上传队列时出错: {ex.Message}", LogHelper.LogType.Error); LogHelper.WriteLogToFile($"[MainWindow] 初始化上传队列时出错: {ex.Message}", LogHelper.LogType.Error);
} }
InitializeClipboardMonitoring();
InitializeFloatingWindowInterceptor();
InitializeGlobalHotkeyManager();
try try
{ {
string savePath = Settings.Automation.AutoSavedStrokesLocation; string savePath = Settings.Automation.AutoSavedStrokesLocation;
@@ -2473,9 +2458,12 @@ namespace Ink_Canvas
if (_pendingStartupAutoUpdateCheck && Settings.Startup?.IsAutoUpdate == true) if (_pendingStartupAutoUpdateCheck && Settings.Startup?.IsAutoUpdate == true)
{ {
_pendingStartupAutoUpdateCheck = false; _pendingStartupAutoUpdateCheck = false;
await Task.Delay(5000); await Task.Delay(3000);
LogHelper.WriteLogToFile("AutoUpdate | Running deferred auto-update check after startup"); Dispatcher.BeginInvoke(new Action(() =>
AutoUpdate(); {
LogHelper.WriteLogToFile("AutoUpdate | Running deferred auto-update check at UI idle");
AutoUpdate();
}), DispatcherPriority.ApplicationIdle);
} }
} }
@@ -649,7 +649,7 @@ namespace Ink_Canvas
ScheduleBrushAutoRestore(); ScheduleBrushAutoRestore();
} }
LogHelper.WriteLogToFile("画笔自动恢复设置已加载", LogHelper.LogType.Event); LogHelper.WriteLogToFile("画笔自动恢复设置已加载", LogHelper.LogType.Trace);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -691,7 +691,7 @@ namespace Ink_Canvas
// 根据设置更新墨迹渐隐控制开关的可见性 // 根据设置更新墨迹渐隐控制开关的可见性
UpdateInkFadeControlVisibility(); UpdateInkFadeControlVisibility();
LogHelper.WriteLogToFile("墨迹渐隐设置已加载", LogHelper.LogType.Event); LogHelper.WriteLogToFile("墨迹渐隐设置已加载", LogHelper.LogType.Trace);
} }
catch (Exception ex) catch (Exception ex)
{ {