From 8147b3972320b1c6ba13a1fe5450f3312e67baa7 Mon Sep 17 00:00:00 2001 From: sparklelcm333 Date: Sun, 7 Jun 2026 14:59:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9C=A8=E7=AA=97=E5=8F=A3=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=89=8D=E8=AE=BE=E7=BD=AE=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E5=B0=BA=E5=AF=B8=EF=BC=8C=E6=B6=88=E9=99=A4=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=97=B6=E5=81=8F=E5=8F=B3=E4=B8=8B=E5=81=8F=E7=A7=BB?= =?UTF-8?q?=20(#9498)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 在窗口初始化前设置保存的尺寸,消除启动时偏右下偏移 WindowStartupLocation="CenterScreen"(来自 XAML)能正确包含 chrome 居中窗口, 但只能使用生效时的 Width/Height。旧代码在 OnLoaded 中设置保存的尺寸为时已晚 (CenterScreen 已应用),随后又用不含 chrome 的手工计算覆盖 Position, 导致窗口每次启动偏右下。 修复方式:在 Initialized 事件中设置保存的 Width/Height。 Avalonia 中 Initialized 在 Show() 过程中触发,早于 WindowStartupLocation 生效, 因此 CenterScreen 能用正确的保存尺寸居中。 同时移除 OnLoaded 中重复的 Width/Height 设置(Initialized 中已设置)。 * Update WindowBase.cs --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com> --- v2rayN/v2rayN.Desktop/Base/WindowBase.cs | 26 ++++++++++++++---------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/v2rayN/v2rayN.Desktop/Base/WindowBase.cs b/v2rayN/v2rayN.Desktop/Base/WindowBase.cs index d1419b30..8379725d 100644 --- a/v2rayN/v2rayN.Desktop/Base/WindowBase.cs +++ b/v2rayN/v2rayN.Desktop/Base/WindowBase.cs @@ -4,6 +4,7 @@ public class WindowBase : ReactiveWindow where TViewMode { public WindowBase() { + Initialized += OnWindowInitialized; Loaded += OnLoaded; } @@ -12,30 +13,33 @@ public class WindowBase : ReactiveWindow where TViewMode throw new NotImplementedException(); } - protected virtual void OnLoaded(object? sender, RoutedEventArgs e) + private void OnWindowInitialized(object? sender, EventArgs e) { try { var sizeItem = ConfigHandler.GetWindowSizeItem(AppManager.Instance.Config, GetType().Name); - if (sizeItem == null) + if (sizeItem is null) { return; } - Width = sizeItem.Width; - Height = sizeItem.Height; + if (sizeItem.Width > 0 && !Width.Equals(sizeItem.Width)) + { + Width = sizeItem.Width; + } - var workingArea = (Screens.ScreenFromWindow(this) ?? Screens.Primary).WorkingArea; - var scaling = (Utils.IsMacOS() ? null : VisualRoot?.RenderScaling) ?? 1.0; - - var x = workingArea.X + ((workingArea.Width - (Width * scaling)) / 2); - var y = workingArea.Y + ((workingArea.Height - (Height * scaling)) / 2); - - Position = new PixelPoint((int)x, (int)y); + if (sizeItem.Height > 0 && !Height.Equals(sizeItem.Height)) + { + Height = sizeItem.Height; + } } catch { } } + protected virtual void OnLoaded(object? sender, RoutedEventArgs e) + { + } + protected override void OnClosed(EventArgs e) { base.OnClosed(e);