feat(插件): 添加应用重启服务接口及实现

添加 IAppRestartService 接口及其实现 AppRestartService,用于插件系统调用应用重启功能
将原 StartupPage 中的重启逻辑提取到 AppRestartHelper 工具类中
在 App.xaml.cs 中注册 AppRestartService 供插件使用
This commit is contained in:
PrefacedCorg
2026-04-21 02:16:58 +08:00
parent cab703b98e
commit 259ce3dd11
5 changed files with 183 additions and 47 deletions
@@ -1,8 +1,8 @@
using Ink_Canvas.Helpers;
using Ink_Canvas.Windows.SettingsViews.Helpers;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows;
using System.Windows.Controls;
@@ -28,52 +28,10 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
_isLoaded = true;
}
private static bool IsRunningAsAdmin()
{
try
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
return false;
}
}
private void RestartApp(bool asAdmin)
{
try
{
App.IsAppExitByUser = true;
(Application.Current as App)?.ReleaseMutexForRestart();
string exePath = Process.GetCurrentProcess().MainModule.FileName;
if (asAdmin)
{
var psi = new ProcessStartInfo(exePath) { UseShellExecute = true, Verb = "runas" };
Process.Start(psi);
}
else
{
Process.Start("explorer.exe", "\"" + exePath + "\"");
}
Application.Current.Shutdown();
}
catch (Exception ex)
{
Debug.WriteLine($"重启应用时出错: {ex.Message}");
}
}
private void LoadSettings()
{
_isLoaded = false;
_isAdmin = IsRunningAsAdmin();
_isAdmin = AppRestartHelper.IsRunningAsAdmin();
try
{
@@ -257,7 +215,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
if (result == MessageBoxResult.Yes)
{
RestartApp(_isAdmin);
AppRestartHelper.RestartWithCurrentPrivileges();
}
}
catch (Exception ex)
@@ -288,7 +246,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
if (result == MessageBoxResult.Yes)
{
App.IsUIAccessTopMostEnabled = true;
RestartApp(true);
AppRestartHelper.RestartAsAdmin();
}
}
catch (Exception ex)
@@ -301,7 +259,7 @@ namespace Ink_Canvas.Windows.SettingsViews.Pages
{
if (sender is Button btn && btn.Tag is bool asAdmin)
{
RestartApp(asAdmin);
AppRestartHelper.RestartApp(asAdmin);
}
}