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

  • 相关阅读:
    spring的ThreadPoolTaskExecutor装饰器传递调用线程信息给线程池中的线程
    文字转语音播报模块(二):JACOB 语音模块
    天空卫士C++ 一面(技术面、61min)
    数据结构与算法之链表-python实现(简单好用)
    期货技术分析难学吗(商品期货学难嘛)
    使用MySQL设计一个“信息管理系统”数据库(1+X Web前端开发中级 例题)
    前端初始化项目切换镜像命令
    python调用java中的jar
    services层和controller层
    HOJ排队打水F601题解
  • 原文地址:https://blog.csdn.net/zhang8593/article/details/139465370