添加关闭窗口程序最小化到通知栏
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="HandyControl" Version="3.5.1" />
|
||||
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
|
||||
<PackageReference Include="iNKORE.UI.WPF.Modern" Version="0.10.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
|
||||
@@ -55,6 +56,7 @@
|
||||
<ItemGroup>
|
||||
<None Remove="Assets\AppIcon.png" />
|
||||
<Resource Include="Assets\AppIcon.png" />
|
||||
<Resource Include="AppIcon2.ico" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -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<MenuBean>(menus); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示主窗口的命令。
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void ShowWindow()
|
||||
{
|
||||
if (Application.Current.MainWindow is Views.MainView mainWindow)
|
||||
{
|
||||
mainWindow.ShowApplication();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出应用程序的命令。
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void ExitApplication()
|
||||
{
|
||||
Application.Current.Shutdown();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加变量表。
|
||||
/// </summary>
|
||||
|
||||
@@ -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 @@
|
||||
</DataTemplate>
|
||||
</Window.Resources>
|
||||
<Grid>
|
||||
<taskbarNotification:TaskbarIcon x:Name="MyNotifyIcon"
|
||||
ToolTipText="设备管理系统"
|
||||
IconSource="/AppIcon2.ico"
|
||||
DoubleClickCommand="{Binding ShowWindowCommand}">
|
||||
<taskbarNotification:TaskbarIcon.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="显示窗口" Command="{Binding ShowWindowCommand}" />
|
||||
<MenuItem Header="退出" Command="{Binding ExitApplicationCommand}" />
|
||||
</ContextMenu>
|
||||
</taskbarNotification:TaskbarIcon.ContextMenu>
|
||||
</taskbarNotification:TaskbarIcon>
|
||||
<ui:NavigationView ExpandedModeThresholdWidth="500"
|
||||
IsTabStop="False"
|
||||
PaneDisplayMode="Left"
|
||||
|
||||
@@ -12,6 +12,8 @@ namespace PMSWPF.Views;
|
||||
/// <summary>
|
||||
/// MainView.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
// 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();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 左边菜单项被点击的事件,切换右边的视图
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user