添加关闭窗口程序最小化到通知栏
This commit is contained in:
@@ -47,6 +47,7 @@ public partial class App : Application
|
|||||||
protected override async void OnStartup(StartupEventArgs e)
|
protected override async void OnStartup(StartupEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
|
ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
||||||
await Host.StartAsync();
|
await Host.StartAsync();
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||||
<PackageReference Include="HandyControl" Version="3.5.1" />
|
<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="iNKORE.UI.WPF.Modern" Version="0.10.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
|
||||||
@@ -55,6 +56,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="Assets\AppIcon.png" />
|
<None Remove="Assets\AppIcon.png" />
|
||||||
<Resource Include="Assets\AppIcon.png" />
|
<Resource Include="Assets\AppIcon.png" />
|
||||||
|
<Resource Include="AppIcon2.ico" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows; // Add this using directive
|
||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.Input; // Add this using directive
|
||||||
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
||||||
using iNKORE.UI.WPF.Modern.Controls;
|
using iNKORE.UI.WPF.Modern.Controls;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@@ -64,6 +66,27 @@ public partial class MainViewModel : ViewModelBase
|
|||||||
dataServices.OnMenuTreeListChanged += (sender, menus) => { Menus = new ObservableCollection<MenuBean>(menus); };
|
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>
|
||||||
/// 添加变量表。
|
/// 添加变量表。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -8,12 +8,15 @@
|
|||||||
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
xmlns:vm="clr-namespace:PMSWPF.ViewModels"
|
||||||
xmlns:mo="clr-namespace:PMSWPF.Models"
|
xmlns:mo="clr-namespace:PMSWPF.Models"
|
||||||
xmlns:hc="https://handyorg.github.io/handycontrol"
|
xmlns:hc="https://handyorg.github.io/handycontrol"
|
||||||
|
xmlns:tb="http://hardcodet.net/taskbar"
|
||||||
|
xmlns:taskbarNotification="http://www.hardcodet.net/taskbar"
|
||||||
Title="设备管理系统"
|
Title="设备管理系统"
|
||||||
Width="1080"
|
Width="1080"
|
||||||
Height="800"
|
Height="800"
|
||||||
WindowStartupLocation="CenterScreen"
|
WindowStartupLocation="CenterScreen"
|
||||||
WindowState="Maximized"
|
WindowState="Maximized"
|
||||||
Loaded="MainView_OnLoaded"
|
Loaded="MainView_OnLoaded"
|
||||||
|
Closing="Window_Closing"
|
||||||
ui:WindowHelper.UseModernWindowStyle="True"
|
ui:WindowHelper.UseModernWindowStyle="True"
|
||||||
ui:WindowHelper.SystemBackdropType="Mica"
|
ui:WindowHelper.SystemBackdropType="Mica"
|
||||||
d:DataContext="{d:DesignInstance vm:MainViewModel}"
|
d:DataContext="{d:DesignInstance vm:MainViewModel}"
|
||||||
@@ -37,6 +40,17 @@
|
|||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</Window.Resources>
|
</Window.Resources>
|
||||||
<Grid>
|
<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"
|
<ui:NavigationView ExpandedModeThresholdWidth="500"
|
||||||
IsTabStop="False"
|
IsTabStop="False"
|
||||||
PaneDisplayMode="Left"
|
PaneDisplayMode="Left"
|
||||||
@@ -49,7 +63,7 @@
|
|||||||
MenuItemsSource="{Binding Menus}"
|
MenuItemsSource="{Binding Menus}"
|
||||||
MenuItemTemplate="{StaticResource NavigationViewMenuItem}"
|
MenuItemTemplate="{StaticResource NavigationViewMenuItem}"
|
||||||
SelectionChanged="NavigationView_SelectionChanged">
|
SelectionChanged="NavigationView_SelectionChanged">
|
||||||
|
|
||||||
|
|
||||||
<ui:NavigationView.AutoSuggestBox>
|
<ui:NavigationView.AutoSuggestBox>
|
||||||
<ui:AutoSuggestBox AutomationProperties.Name="Search">
|
<ui:AutoSuggestBox AutomationProperties.Name="Search">
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ namespace PMSWPF.Views;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// MainView.xaml 的交互逻辑
|
/// MainView.xaml 的交互逻辑
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
// using Hardcodet.NotifyIcon.Wpf;
|
||||||
|
|
||||||
public partial class MainView : Window
|
public partial class MainView : Window
|
||||||
{
|
{
|
||||||
private readonly DataServices _dataServices;
|
private readonly DataServices _dataServices;
|
||||||
@@ -24,8 +26,26 @@ public partial class MainView : Window
|
|||||||
_dataServices = dataServices;
|
_dataServices = dataServices;
|
||||||
DataContext = _viewModel;
|
DataContext = _viewModel;
|
||||||
NlogHelper.Info("主界面加载成功");
|
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>
|
||||||
/// 左边菜单项被点击的事件,切换右边的视图
|
/// 左边菜单项被点击的事件,切换右边的视图
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user