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

相关推荐
逸模4 小时前
告别熬夜手工整理台账,逸模智能归集实现项目数据自动化存档
大数据·运维·人工智能·笔记·其他·信息可视化·自动化
audyxiao0016 小时前
ICLR 2026论文分享 | WorldGym:用世界模型打造机器人策略评估新范式
大数据·人工智能·大模型·智能体·世界模型
Rubin智造社7 小时前
Anthropic安全白皮书2|三级成熟度模型:你的AI智能体该配哪级安全?
大数据·安全·沙箱隔离·零信任成熟度模型·三级安全框架·jit权限·不可变审计
ACP广源盛139246256737 小时前
GSV2221 显示转换芯片@ACP#赋能 RTX Spark 端侧 AI 设备,构建多屏全模态视觉交互新生态
大数据·人工智能·嵌入式硬件·gpt·spark·电脑·音视频
字节跳动开源7 小时前
你的 Agent 每次都“失忆”?这个工具彻底治好了我的前端开发焦虑
大数据·开源·agent
APItesterCris9 小时前
实战教程:借助 Open Claw + 淘宝商品 API,低成本实现电商自动化监控与智能选品
大数据·运维·自动化
团象科技9 小时前
从一线运营场景观察 海外云 独立站的跨境效能释放实践路径
大数据·人工智能
宸津-代码粉碎机9 小时前
Spring AI企业级实战|从RAG优化到Agent多工具调度
java·大数据·人工智能·后端·python·spring
INFINI Labs9 小时前
Elasticsearch 6/7/8 到 Easysearch 2.x 迁移指南
大数据·elasticsearch·mybatis·向量·snapshot
小柒儿3369 小时前
汪进进:深水区里以质立身,做长期价值的践行者
大数据·人工智能