From c473c500d850bf9c7988d5a15ae3734f9e7fb884 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Mon, 7 Jul 2025 14:01:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=B3=E9=97=AD=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E7=A8=8B=E5=BA=8F=E6=9C=80=E5=B0=8F=E5=8C=96=E5=88=B0?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 1 + PMSWPF.csproj | 2 ++ ViewModels/MainViewModel.cs | 23 +++++++++++++++++++++++ Views/MainView.xaml | 16 +++++++++++++++- Views/MainView.xaml.cs | 20 ++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/App.xaml.cs b/App.xaml.cs index a24a631..fab722e 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -47,6 +47,7 @@ public partial class App : Application protected override async void OnStartup(StartupEventArgs e) { base.OnStartup(e); + ShutdownMode = ShutdownMode.OnExplicitShutdown; await Host.StartAsync(); try diff --git a/PMSWPF.csproj b/PMSWPF.csproj index 3e9f55c..fbed3fc 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -15,6 +15,7 @@ + @@ -55,6 +56,7 @@ + diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index c29b0ee..123bfd0 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -1,5 +1,7 @@ using System.Collections.ObjectModel; +using System.Windows; // Add this using directive using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; // Add this using directive using iNKORE.UI.WPF.Modern.Common.IconKeys; using iNKORE.UI.WPF.Modern.Controls; using Microsoft.Extensions.DependencyInjection; @@ -64,6 +66,27 @@ public partial class MainViewModel : ViewModelBase dataServices.OnMenuTreeListChanged += (sender, menus) => { Menus = new ObservableCollection(menus); }; } + /// + /// 显示主窗口的命令。 + /// + [RelayCommand] + private void ShowWindow() + { + if (Application.Current.MainWindow is Views.MainView mainWindow) + { + mainWindow.ShowApplication(); + } + } + + /// + /// 退出应用程序的命令。 + /// + [RelayCommand] + private void ExitApplication() + { + Application.Current.Shutdown(); + } + /// /// 添加变量表。 /// diff --git a/Views/MainView.xaml b/Views/MainView.xaml index f2bcbc7..21fcf00 100644 --- a/Views/MainView.xaml +++ b/Views/MainView.xaml @@ -8,12 +8,15 @@ xmlns:vm="clr-namespace:PMSWPF.ViewModels" xmlns:mo="clr-namespace:PMSWPF.Models" xmlns:hc="https://handyorg.github.io/handycontrol" + xmlns:tb="http://hardcodet.net/taskbar" + xmlns:taskbarNotification="http://www.hardcodet.net/taskbar" Title="设备管理系统" Width="1080" Height="800" WindowStartupLocation="CenterScreen" WindowState="Maximized" Loaded="MainView_OnLoaded" + Closing="Window_Closing" ui:WindowHelper.UseModernWindowStyle="True" ui:WindowHelper.SystemBackdropType="Mica" d:DataContext="{d:DesignInstance vm:MainViewModel}" @@ -37,6 +40,17 @@ + + + + + + + + - + diff --git a/Views/MainView.xaml.cs b/Views/MainView.xaml.cs index 02b0850..11c70ee 100644 --- a/Views/MainView.xaml.cs +++ b/Views/MainView.xaml.cs @@ -12,6 +12,8 @@ namespace PMSWPF.Views; /// /// MainView.xaml 的交互逻辑 /// +// using Hardcodet.NotifyIcon.Wpf; + public partial class MainView : Window { private readonly DataServices _dataServices; @@ -24,8 +26,26 @@ public partial class MainView : Window _dataServices = dataServices; DataContext = _viewModel; NlogHelper.Info("主界面加载成功"); + + // Set the NotifyIcon's DataContext to the ViewModel + MyNotifyIcon.DataContext = _viewModel; } + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + // Hide the window instead of closing it + e.Cancel = true; + Hide(); + } + + public void ShowApplication() + { + Show(); + WindowState = WindowState.Normal; + Activate(); + } + + /// /// 左边菜单项被点击的事件,切换右边的视图 ///