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


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

    2、在App.xaml中创建托盘界面,代码是写在 App.xaml 里面

    注意在application中一定要加入这一行代码: xmlns:tb="http://www.hardcodet.net/taskbar"

    然后在中加入如下代码
     

    1. "false" x:Key="SysTrayMenu">
    2. "25" Header="显示界面" Command="{Binding NotifyCommand}" CommandParameter="1">
    3. "25" Header="隐藏界面" Command="{Binding NotifyCommand}" CommandParameter="0">
    4. "25" Header="退出服务" Command="{Binding NotifyCommand}" CommandParameter="99">
    5. "Taskbar" ToolTipText=""
    6. DoubleClickCommand="{Binding NotifyCommand}" DoubleClickCommandParameter="1"
    7. ContextMenu="{StaticResource SysTrayMenu}" IconSource="/jqsw.ico">
    8. "LightYellow" CornerRadius="5" Opacity="0.8" Padding="10">
    9. "Vertical">
    10. "温湿度数据采集服务网口版" Foreground="Red"/>

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

    1. internal class ViewModelBase : INotifyPropertyChanged
    2. {
    3. public event PropertyChangedEventHandler PropertyChanged ;
    4. protected void RaisePropertyChanged(string property)
    5. {
    6. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
    7. }
    8. }
    9. internal class TaskbarIconViewModel : ViewModelBase
    10. {
    11. private string systemTime;
    12. public string SystemTime
    13. {
    14. get { return systemTime; }
    15. set { systemTime = value; RaisePropertyChanged("SystemTime"); }
    16. }
    17. public DelegateCommand NotifyCommand
    18. {
    19. get
    20. {
    21. return new DelegateCommand((type) =>
    22. {
    23. if (type.ToString() == "0")
    24. Application.Current.MainWindow.Hide();
    25. if (type.ToString() == "1")
    26. {
    27. Application.Current.MainWindow.Show();
    28. Application.Current.MainWindow.Activate();
    29. }
    30. if (type.ToString() == "99")
    31. Application.Current.Shutdown();
    32. });
    33. }
    34. }
    35. }

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

    1. private static System.Threading.Mutex mutex;
    2. protected override void OnStartup(StartupEventArgs e)
    3. {
    4. mutex = new System.Threading.Mutex(true, "TemCollSrvTwo");
    5. if (mutex.WaitOne(0, false))
    6. {
    7. base.OnStartup(e);
    8. }
    9. else
    10. {
    11. MessageBox.Show("程序已经在运行!", "提示");
    12. this.Shutdown();
    13. }
    14. mTaskbarIcon = (TaskbarIcon)FindResource("Taskbar");
    15. mTaskbarIcon.DataContext = new TaskbarIconViewModel();
    16. }
    17. public static TaskbarIcon mTaskbarIcon;
    18. 4031

    5、效果展示

  • 相关阅读:
    Linux软件:如何安装Nginx服务
    短视频账号矩阵系统源码
    JAVA计算机毕业设计房产客户信息管理系统Mybatis+源码+数据库+lw文档+系统+调试部署
    【matplotlib基础】--手绘风格
    Python开源项目周排行 2023年第35周
    代码随想录动态规划——背包问题总结篇
    oracle循环
    JavaScript之运算符、表达式、流程控制语句、if、for循环、Switch、continue和break
    【iMessage苹果推】群发软件您的通行证类型ID并拒绝您的通行证productable两个父协议
    使用 kind 搭建 Kubernetes学习环境
  • 原文地址:https://blog.csdn.net/zhang8593/article/details/139465370