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

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

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

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

然后在<Application.Resources>中加入如下代码

cs 复制代码
    <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类

cs 复制代码
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中写入如下代码:

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

相关推荐
阳艳讲ai7 分钟前
中小企业AI技术方案选型评估框架:四类架构与评估指标分析
大数据·人工智能·企业ai培训·ai获客·九尾狐ai·ai应用工具
易观Analysys12 分钟前
中美Agent生态的路径差异——《重构与崛起——OpenClaw时代的中国Agent产业生态报告》解读三
大数据·人工智能
zhojiew1 小时前
AWS云上使用Redshift Test Drive进行负载重放测试的实践
大数据·redshift
移动云开发者联盟1 小时前
存智赋能 共筑AI存储新生态,移动云聚力技术创新夯实AI数据基石
大数据·人工智能
TDengine (老段)1 小时前
TDengine 数据库创建与参数详解
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
面向Google编程1 小时前
从零学习Kafka:调优
大数据·kafka
不是株2 小时前
ElasticSearch
大数据·elasticsearch·搜索引擎
随身数智备忘录2 小时前
拆解合理化建议系统的三大流程,合理化建议如何解决建议征集难与落地慢
大数据·人工智能
逸Y 仙X2 小时前
文章三十四:ElasticSearch Script脚本实战
大数据·elasticsearch·搜索引擎·全文检索
倔强的石头1062 小时前
【Linux 指南】文件系统系列(三):Ext系统核心实现 —— 从块组到 inode 与数据块映射全解析
大数据·linux·运维