diff --git a/v2rayN/ServiceLib/Enums/EViewAction.cs b/v2rayN/ServiceLib/Enums/EViewAction.cs index c37e1212..4db320d1 100644 --- a/v2rayN/ServiceLib/Enums/EViewAction.cs +++ b/v2rayN/ServiceLib/Enums/EViewAction.cs @@ -14,6 +14,7 @@ ShareSub, ShareServer, ShowHideWindow, + ShowInTaskbar, ScanScreenTask, ScanImageTask, Shutdown, diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 3be5dcaa..991fabbd 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -218,7 +218,6 @@ namespace ServiceLib.ViewModels } await Reload(); - await AutoHideStartup(); Locator.Current.GetService()?.RefreshRoutingsMenu(); } @@ -331,6 +330,7 @@ namespace ServiceLib.ViewModels public void ShowHideWindow(bool? blShow) { _updateView?.Invoke(EViewAction.ShowHideWindow, blShow); + _updateView?.Invoke(EViewAction.ShowInTaskbar, blShow); } #endregion Actions @@ -586,14 +586,6 @@ namespace ServiceLib.ViewModels await CoreHandler.Instance.CoreStop(); } - private async Task AutoHideStartup() - { - if (_config.UiItem.AutoHideStartup) - { - ShowHideWindow(false); - } - } - #endregion core job #region Presets diff --git a/v2rayN/v2rayN.Desktop/App.axaml.cs b/v2rayN/v2rayN.Desktop/App.axaml.cs index 898de4e8..53586ec5 100644 --- a/v2rayN/v2rayN.Desktop/App.axaml.cs +++ b/v2rayN/v2rayN.Desktop/App.axaml.cs @@ -1,4 +1,5 @@ using Avalonia; +using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using Splat; @@ -30,10 +31,20 @@ public partial class App : Application { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - AppHandler.Instance.InitComponents(); - desktop.Exit += OnExit; - desktop.MainWindow = new MainWindow(); + + // 先完成组件初始化 + AppHandler.Instance.InitComponents(); + var mainWindow = new MainWindow(); + if (AppHandler.Instance.Config.UiItem.AutoHideStartup) + { + // 创建主窗口实例但暂不显示 + mainWindow.ShowInTaskbar = false; + mainWindow.WindowState = WindowState.Minimized; + mainWindow.IsVisible = false; + + } + desktop.MainWindow = mainWindow; } base.OnFrameworkInitializationCompleted(); diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index eebc9362..55b63391 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -207,6 +207,11 @@ namespace v2rayN.Desktop.Views DispatcherPriority.Default); break; + case EViewAction.ShowInTaskbar: + Dispatcher.UIThread.Post(() => + showInTaskbar((bool?)obj), + DispatcherPriority.Default); + break; case EViewAction.DispatcherStatistics: if (obj is null) return false; Dispatcher.UIThread.Post(() => @@ -383,6 +388,12 @@ namespace v2rayN.Desktop.Views #region UI + private void showInTaskbar(bool? blShow) + { + var bl = blShow ?? this.WindowState == WindowState.Normal; + this.ShowInTaskbar = bl; + } + public void ShowHideWindow(bool? blShow) { var bl = blShow ?? !_config.UiItem.ShowInTaskbar;