当前位置: 首页 > news >正文

wpf工程中加入Hardcodet.NotifyIcon.Wpf生成托盘

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:Key="Taskbar" ToolTipText=""DoubleClickCommand="{Binding NotifyCommand}"  DoubleClickCommandParameter="1"ContextMenu="{StaticResource SysTrayMenu}" IconSource="/jqsw.ico"><tb:TaskbarIcon.TrayToolTip><Border Background="LightYellow" CornerRadius="5" Opacity="0.8" Padding="10"><StackPanel Orientation="Vertical"><TextBlock Text="温湿度数据采集服务网口版" Foreground="Red"/><!--<TextBlock Text="{Binding SystemTime}" Foreground="Red"/>--></StackPanel></Border></tb:TaskbarIcon.TrayToolTip></tb:TaskbarIcon></Application.Resources>

3、创建ViewModelBase类。再创建TaskbarIconViewModel类继承ViewModelBase类

internal class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler  PropertyChanged ;protected void RaisePropertyChanged(string property){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));}}internal class TaskbarIconViewModel : ViewModelBase{private string systemTime;public string SystemTime{get { return systemTime; }set { systemTime = value; RaisePropertyChanged("SystemTime"); }}public DelegateCommand NotifyCommand{get{return new DelegateCommand((type) =>{if (type.ToString() == "0")Application.Current.MainWindow.Hide();if (type.ToString() == "1"){Application.Current.MainWindow.Show();Application.Current.MainWindow.Activate();}if (type.ToString() == "99")Application.Current.Shutdown();});}}} 

、在App.xaml.cs中写入如下代码:

private static System.Threading.Mutex mutex;protected override void OnStartup(StartupEventArgs e){mutex = new System.Threading.Mutex(true, "TemCollSrvTwo");if (mutex.WaitOne(0, false)){base.OnStartup(e);}else{MessageBox.Show("程序已经在运行!", "提示");this.Shutdown();} mTaskbarIcon = (TaskbarIcon)FindResource("Taskbar");mTaskbarIcon.DataContext = new TaskbarIconViewModel();}public static TaskbarIcon  mTaskbarIcon;4031

5、效果展示

http://www.lryc.cn/news/364638.html

相关文章:

  • keil下载及安装(社区版本)
  • python书上的动物是啥
  • 数据库管理-第198期 升级Oracle ACE Pro,新赛季继续努力(20240605)
  • 华为坤灵交换机S300, S500, S210,S220, S200, S310 如何WEB抓包
  • 【亚马逊云科技 CSDN 联合巨献】 「对话AI 构建者:从基础到应用的 LLM 全景培训」 限时免费!
  • 【AI大模型】Function Calling
  • 零钱兑换 - LeetCode 热题 85
  • 基于web的垃圾分类回收系统的设计
  • 优化你的WordPress网站:内链建设与Link Whisper Pro插件的利用
  • spring中那些地方使用了反射
  • 1 机器人软件开发学习所需通用技术栈(一)
  • Java(十二)——Comparable接口与Comparator接口
  • Nvidia Jetson/Orin +FPGA+AI大算力边缘计算盒子:轨道交通监控系统
  • 笔记 | 软件工程01:从程序到软件
  • 废品回收小程序开发,助力商家拓展回收市场
  • JVM类加载机制和双亲委派
  • 【PyCharm】无法创建虚拟环境,提示:has no attribute CPython3macOsBrew
  • 华为OD刷题C卷 - 每日刷题 12(数组连续和,求最多可以派出多少支团队)
  • 2.1 初识Windows程序
  • EDI系统的使用场景
  • 韩国Neowine推出第三代强加密芯片ALPU-CV
  • golang结构与接口方法实现与交互使用示例
  • C# 判断字符串不等于空的示例
  • 直方图中最大的矩形
  • 分布式锁redisson
  • 将小爱音箱接入 ChatGPT 和豆包ai改造成专属语音助手
  • 短网址生成原理及使用
  • C#调用word组件转pdf,遇到视图保护解决方法
  • NAT端口映射,实现外网访问内网服务器
  • 【面试笔记】嵌入式软件工程师,汽车电子软件相关