diff --git a/.gitignore b/.gitignore index 234fc04..1f76942 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,15 @@ coverage/ .vite/ *.tsbuildinfo +# ---------- .NET / WPF(desktop/) ---------- +bin/ +obj/ +publish/ +artifacts/ +.vs/ +*.user +*.suo + # ---------- 运行期 / 日志 ---------- # backend 的 logs/、多实例自测时的 logs-test/、脚本产出的 run/ logs/ diff --git a/desktop/App.xaml b/desktop/App.xaml new file mode 100644 index 0000000..6f476eb --- /dev/null +++ b/desktop/App.xaml @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/desktop/App.xaml.cs b/desktop/App.xaml.cs new file mode 100644 index 0000000..e47265c --- /dev/null +++ b/desktop/App.xaml.cs @@ -0,0 +1,64 @@ +using System.Windows; +using YuncangTrace.LocalService.Services; + +namespace YuncangTrace.LocalService; + +/// +/// 应用入口 +/// +/// 两件事:**单实例**(协议被点开时系统会再启动一次本程序,要把参数转给已运行的实例) +/// 和**把窗口拉起来**。 +/// +public partial class App : Application +{ + private SingleInstance? _single; + private MainWindow? _window; + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + _single = new SingleInstance("YuncangTrace.LocalService", OnSecondInstanceArgs); + + // 已经有一个实例在跑:把自己的参数(可能是 yuncang://open?code=xxx)转过去,然后退出 + if (!_single.IsFirstInstance) + { + _single.SendArgs(e.Args); + Shutdown(); + return; + } + + _window = new MainWindow(); + MainWindow = _window; + _window.Show(); + + // 起命名管道监听(接收后续「再次启动」带来的参数) + _single.StartListening(); + + // 本次就是被协议拉起来的:直接进入「按 code 查信息」 + var launchUrl = ProtocolHelper.FirstLaunchUrl(e.Args); + if (launchUrl is not null) _window.HandleLaunchUrl(launchUrl); + } + + /// 另一个实例把参数送过来了(工作线程回调,切回 UI 线程) + private void OnSecondInstanceArgs(string[] args) + { + Dispatcher.Invoke(() => + { + if (MainWindow is MainWindow window) + { + if (window.WindowState == WindowState.Minimized) window.WindowState = WindowState.Normal; + window.Activate(); + + var url = ProtocolHelper.FirstLaunchUrl(args); + if (url is not null) window.HandleLaunchUrl(url); + } + }); + } + + protected override void OnExit(ExitEventArgs e) + { + _single?.Dispose(); + base.OnExit(e); + } +} diff --git a/desktop/MainWindow.xaml b/desktop/MainWindow.xaml new file mode 100644 index 0000000..ec3291f --- /dev/null +++ b/desktop/MainWindow.xaml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +