1、在项目中用nuget引入Hardcodet.NotifyIcon.Wpf。如下图所示。

2、在App.xaml中创建托盘界面,代码是写在 App.xaml 里面
注意在application中一定要加入这一行代码: xmlns:tb="http://www.hardcodet.net/taskbar"
然后在<Application.Resources>中加入如下代码
<Application.Resources>
<ContextMenu x:Shared="false" x:Key="SysTrayMenu">
<MenuItem Height="25" Header="显示界面" Command="{Binding NotifyCommand}" CommandParameter="1">
</MenuItem>
<MenuItem Height="25" Header="隐藏界面" Command="{Binding NotifyCommand}" CommandParameter="0">
</MenuItem>
<Separator />
<MenuItem Height="25" Header="退出服务" Command="{Binding NotifyCommand}" CommandParameter="99">
</MenuItem>
</ContextMenu>
<tb:TaskbarIcon x:K

本文介绍了如何在WPF项目中通过nuget包Hardcodet.NotifyIcon.Wpf创建系统托盘图标,包括在App.xaml中配置托盘菜单,创建ViewModelBase和TaskbarIconViewModel以处理命令,以及在App.xaml.cs中设置数据上下文和互斥锁确保单例运行。
最低0.47元/天 解锁文章
419





