Files
Windows-Gotify-Client/App.xaml.cs
T
KennethCheng e53c0585f7 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 自然重连)
2026-09-06 20:22:54 +08:00

150 lines
4.6 KiB
C#

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
{
private static Mutex? _mutex;
private Services.ConfigService? _configService;
private Services.Logger? _logger;
private Services.StateService? _stateService;
private Services.GotifyApiService? _apiService;
private Services.GotifyWebSocketService? _webSocketService;
private Services.NotificationService? _notificationService;
private Tray.TrayIconService? _trayService;
private MainWindow? _mainWindow;
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_mutex = new Mutex(true, "WindowsGotifyAgent-SingleInstance", out bool createdNew);
if (!createdNew)
{
Shutdown();
return;
}
_logger = new Services.Logger();
_configService = new Services.ConfigService(_logger);
var config = await _configService.LoadAsync();
_stateService = new Services.StateService(_logger);
_apiService = new Services.GotifyApiService(config, _logger);
_notificationService = new Services.NotificationService(config, _logger);
try
{
// NotificationInvoked 必须在 Register 之前注册。
AppNotificationManager.Default.NotificationInvoked += OnNotificationInvoked;
AppNotificationManager.Default.Register();
_logger.Info("Windows App SDK notification manager registered.");
}
catch (Exception ex)
{
_logger.Error("Notification registration failed.", ex);
}
_mainWindow = new MainWindow();
_mainWindow.Hide();
_trayService = new Tray.TrayIconService(
onShowSettings: () => _mainWindow.ShowFromTray(),
onTest: async () => await SendTestNotificationAsync(),
onReconnect: () => _webSocketService?.RequestReconnect(),
onExit: () => Shutdown()
);
_webSocketService = new Services.GotifyWebSocketService(
config,
_logger,
_stateService,
_apiService,
_notificationService,
status => _trayService.SetStatus(status));
_mainWindow.Initialize(
config,
_configService,
_logger,
_trayService,
_webSocketService,
_apiService);
if (config.Startup.Enabled)
{
Utils.StartupManager.EnableCurrentUserStartup(_logger);
}
await _webSocketService.StartAsync();
}
private async Task SendTestNotificationAsync()
{
if (_notificationService is null)
return;
await _notificationService.ShowAsync(
new Models.GotifyMessage
{
Id = 0,
AppId = 0,
Message = "Windows Gotify Agent 测试通知。",
Title = "Gotify 测试",
Priority = 5,
Date = DateTimeOffset.UtcNow
});
}
private void OnNotificationInvoked(
AppNotificationManager sender,
AppNotificationActivatedEventArgs args)
{
Dispatcher.Invoke(() =>
{
try
{
// 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))
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
else
{
_mainWindow?.ShowFromTray();
}
}
catch (Exception ex)
{
_logger?.Error("Notification invocation failed.", ex);
}
});
}
protected override void OnExit(ExitEventArgs e)
{
try
{
_webSocketService?.Stop();
_trayService?.Dispose();
AppNotificationManager.Default.Unregister();
AppNotificationManager.Default.NotificationInvoked -= OnNotificationInvoked;
}
catch (Exception ex)
{
_logger?.Error("Application cleanup failed.", ex);
}
base.OnExit(e);
}
}