From e53c0585f7f934bf5d91f8f0025ba665c3c387d8 Mon Sep 17 00:00:00 2001 From: kennethcheng Date: Sun, 6 Sep 2026 20:22:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20SDK=202.4.0=20+=20W?= =?UTF-8?q?PF/WinForms=20=E5=8F=8C=E5=BC=80=E4=B8=8B=E7=9A=84=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dotnet 8.0.424 SDK + Microsoft.WindowsAppSDK 2.4.0 实测 build 通过(0 警告 0 错误)。 修复内容: - csproj:TargetFramework 从 net8.0-windows 改为 net8.0-windows10.0.19041.0,新增 SupportedOSPlatformVersion=10.0.17763.0(WindowsAppSDK 2.4.0 强制要求 TFM 带 Windows 版本号) - 4 个 Services 文件加 using System.IO(Logger/ConfigService/StateService/GotifyWebSocketService)。WPF+WinForms 双开项目模板不自动 import System.IO - GotifyApiService 加 using System.Net.Http(同上,HttpClient 不在 implicit usings) - App.xaml.cs:using Application = System.Windows.Application(WPF/WinForms Application 撞名) - MainWindow.xaml.cs:using MessageBox = System.Windows.MessageBox(同上,MessageBox 撞名) - App.xaml.cs:SendTestNotificationAsync 里 Date 从 .UtcNow.ToUnixTimeMilliseconds() 改回 .UtcNow(原 plan 写法有 bug,long 塞不进 DateTimeOffset) - App.xaml.cs:OnNotificationInvoked 重写:WindowsAppSDK 2.4.0 把 args.Arguments 从 string 改成 IDictionary,改用 TryGetValue("url", out var url) 取参数 - NotificationService:AddArgument 从单参 "url=escaped" 改成两参 ("url", url)。WindowsAppSDK 2.4.0 不再支持单参 "key=value" 写法 - GotifyWebSocketService:删除 _forceReconnect 字段(写了不读,改成 RequestReconnect 只调 socket.Abort 让外层 RunLoopAsync 自然重连) --- App.xaml.cs | 10 ++++++---- MainWindow.xaml.cs | 3 +++ Services/ConfigService.cs | 1 + Services/GotifyApiService.cs | 1 + Services/GotifyWebSocketService.cs | 5 ++--- Services/Logger.cs | 2 ++ Services/NotificationService.cs | 5 +++-- Services/StateService.cs | 1 + WindowsGotify.csproj | 3 ++- 9 files changed, 21 insertions(+), 10 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 00944fb..81c3a96 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -2,6 +2,9 @@ using System.Diagnostics; using System.Windows; using Microsoft.Windows.AppNotifications; +// 双开 WPF + WinForms 时,Application 在两个命名空间都有,用别名强制走 WPF 的。 +using Application = System.Windows.Application; + namespace WindowsGotify; public partial class App : Application @@ -94,7 +97,7 @@ public partial class App : Application Message = "Windows Gotify Agent 测试通知。", Title = "Gotify 测试", Priority = 5, - Date = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + Date = DateTimeOffset.UtcNow }); } @@ -106,10 +109,9 @@ public partial class App : Application { try { - var arguments = args.Arguments; - if (arguments.StartsWith("url=", StringComparison.OrdinalIgnoreCase)) + // Windows App SDK 2.4.0:args.Arguments 已经是 IDictionary,由 AddArgument(key, value) 注入 + if (args.Arguments.TryGetValue("url", out var url) && !string.IsNullOrWhiteSpace(url)) { - var url = Uri.UnescapeDataString(arguments[4..]); Process.Start(new ProcessStartInfo { FileName = url, diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 2ed01d9..5e75b7e 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -6,6 +6,9 @@ using WindowsGotify.Models; using WindowsGotify.Services; using WindowsGotify.Tray; +// 双开 WPF + WinForms 时,MessageBox 在两个命名空间都有,用别名强制走 WPF 的。 +using MessageBox = System.Windows.MessageBox; + namespace WindowsGotify; public partial class MainWindow : Window diff --git a/Services/ConfigService.cs b/Services/ConfigService.cs index 6ef239f..2c08a2d 100644 --- a/Services/ConfigService.cs +++ b/Services/ConfigService.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Text.Json; using WindowsGotify.Models; diff --git a/Services/GotifyApiService.cs b/Services/GotifyApiService.cs index a98485f..e7b7f44 100644 --- a/Services/GotifyApiService.cs +++ b/Services/GotifyApiService.cs @@ -1,3 +1,4 @@ +using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json; using WindowsGotify.Models; diff --git a/Services/GotifyWebSocketService.cs b/Services/GotifyWebSocketService.cs index 35f8df9..577c734 100644 --- a/Services/GotifyWebSocketService.cs +++ b/Services/GotifyWebSocketService.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Net.WebSockets; using System.Text; using System.Text.Json; @@ -18,7 +19,6 @@ public sealed class GotifyWebSocketService private readonly object _socketLock = new(); private ClientWebSocket? _socket; private bool _running; - private bool _forceReconnect; /// /// 收到一条新消息(已去重、未越界)时触发,UI 用来追加到历史列表。 @@ -53,7 +53,7 @@ public sealed class GotifyWebSocketService public void RequestReconnect() { - _forceReconnect = true; + // 直接 Abort 当前 socket,ConnectAndReceiveAsync 会抛 → 外层 RunLoopAsync 自动重连 lock (_socketLock) { try @@ -123,7 +123,6 @@ public sealed class GotifyWebSocketService if (!_running || _lifetimeCts.IsCancellationRequested) break; - _forceReconnect = false; _logger.Info($"Reconnect after {delay}s."); _statusChanged($"{delay}s 后重连"); diff --git a/Services/Logger.cs b/Services/Logger.cs index aecabab..aca27c6 100644 --- a/Services/Logger.cs +++ b/Services/Logger.cs @@ -1,3 +1,5 @@ +using System.IO; + namespace WindowsGotify.Services; public sealed class Logger diff --git a/Services/NotificationService.cs b/Services/NotificationService.cs index bcd930d..cd5edbe 100644 --- a/Services/NotificationService.cs +++ b/Services/NotificationService.cs @@ -44,11 +44,12 @@ public sealed class NotificationService : AppNotificationSoundEvent.Default); } - // 把 extras 里的 client::notification.click.url 透传给通知,让 App.OnNotificationInvoked 收到 url=... + // 把 extras 里的 client::notification.click.url 透传给通知,让 App.OnNotificationInvoked 收到 + // Windows App SDK 2.4.0 的 AddArgument 是 (key, value) 两参,跟 1.x 的单参 "key=value" 不一样 var clickUrl = message.ClickUrl; if (!string.IsNullOrWhiteSpace(clickUrl)) { - builder.AddArgument("url=" + Uri.EscapeDataString(clickUrl)); + builder.AddArgument("url", clickUrl); } var notification = builder.BuildNotification(); diff --git a/Services/StateService.cs b/Services/StateService.cs index 7335e46..f1978d7 100644 --- a/Services/StateService.cs +++ b/Services/StateService.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Text.Json; namespace WindowsGotify.Services; diff --git a/WindowsGotify.csproj b/WindowsGotify.csproj index df780bc..6be2875 100644 --- a/WindowsGotify.csproj +++ b/WindowsGotify.csproj @@ -2,7 +2,8 @@ WinExe - net8.0-windows + net8.0-windows10.0.19041.0 + 10.0.17763.0 true true enable