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

相关推荐
Qyr996 小时前
2026-2032直接芯片液冷板市场爆发式增长:AI算力浪潮下的热管理核心赛道
大数据·人工智能
蓝色的风-20267 小时前
国产算力双雄对决:曙光8000十万卡集群 vs 华为昇腾950超节点深度解析
大数据·人工智能·自然语言处理
泛联新安8 小时前
软件定义汽车时代,如何让AI研发“可信”?——泛联新安构建汽车企业级可信AI体系的落地路径
大数据·人工智能·安全·网络安全·汽车·漏洞挖掘·代码安全
杨云龙UP8 小时前
TDengine 3.4.2.8 Community 三节点三副本生产集群部署实战(DNode/MNode/taosAdapter/Explorer)
大数据·linux·运维·数据库·tdengine·时序库
SelectDB技术团队9 小时前
一条日志两套引擎的账:把 Elasticsearch 检索与分析合并到同一份数据的落地写法
大数据·数据库·elasticsearch·搜索引擎·全文检索·日志·apache doris
大大大大晴天9 小时前
每天认识一个组件:远端数据服务Apache Celeborn
大数据
幂律智能10 小时前
海外业务不同,合同系统该怎么建?
大数据·人工智能
Leo.yuan10 小时前
数据开发即质量检测:FineDataLink 5.0 在生产数据链路中的一体化实践
大数据
哲霖软件10 小时前
非标机械设备公司怎么做信息化?破解通用ERP水土不服难题
大数据·运维
IT毕设梦工厂10 小时前
计算机毕业设计选题推荐:基于大数据的杭州亚运会社交媒体互动数据可视化分析|毕业设计选题|计算机毕设|选题推荐|毕设指导|项目定制|源码|高质量项目
大数据·hadoop·信息可视化·数据挖掘·数据分析·课程设计·数据分析数据可视化