• 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、效果展示

  • 相关阅读:
    flutter iOS 视频mov格式转MP4格式
    实战:如何编写一个 OpenTelemetry Extensions
    图解LeetCode——1710. 卡车上的最大单元数(难度:简单)
    MySQL导入导出、视图、索引、执行计划
    AI如何修改画布尺寸? ai怎么设置画布大小_AI教程自学网
    一篇了解如何优雅地处理重复请求
    图的应用(最小生成树,最短路径,有向无环图)
    算法|每日一题|倍数求和|容斥原理
    【JVM内存区域及创建对象的过程】
    Target of Evaluation(TOE)概述
  • 原文地址:https://blog.csdn.net/zhang8593/article/details/139465370