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(); + } + + /// /// 左边菜单项被点击的事件,切换右边的视图 ///