fix: 修复 SDK 2.4.0 + WPF/WinForms 双开下的编译错误

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<string,string>,改用 TryGetValue("url", out var url) 取参数

- NotificationService:AddArgument 从单参 "url=escaped" 改成两参 ("url", url)。WindowsAppSDK 2.4.0 不再支持单参 "key=value" 写法

- GotifyWebSocketService:删除 _forceReconnect 字段(写了不读,改成 RequestReconnect 只调 socket.Abort 让外层 RunLoopAsync 自然重连)
This commit is contained in:
2026-09-06 20:22:54 +08:00
parent c2f44767a6
commit e53c0585f7
9 changed files with 21 additions and 10 deletions
+6 -4
View File
@@ -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<string, string>,由 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,
+3
View File
@@ -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
+1
View File
@@ -1,3 +1,4 @@
using System.IO;
using System.Text.Json;
using WindowsGotify.Models;
+1
View File
@@ -1,3 +1,4 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using WindowsGotify.Models;
+2 -3
View File
@@ -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;
/// <summary>
/// 收到一条新消息(已去重、未越界)时触发,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 后重连");
+2
View File
@@ -1,3 +1,5 @@
using System.IO;
namespace WindowsGotify.Services;
public sealed class Logger
+3 -2
View File
@@ -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();
+1
View File
@@ -1,3 +1,4 @@
using System.IO;
using System.Text.Json;
namespace WindowsGotify.Services;
+2 -1
View File
@@ -2,7 +2,8 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<SupportedOSPlatformVersion>10.0.17763.0</SupportedOSPlatformVersion>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>