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

相关推荐
萤丰信息2 分钟前
技术赋能安全:智慧工地构建城市建设新防线
java·大数据·开发语言·人工智能·智慧城市·智慧工地
Viking_bird1 小时前
Apache Spark 3.2.0 开发测试环境部署指南
大数据·分布式·ajax·spark·apache
用户199701080182 小时前
抖音商品列表API技术文档
大数据·数据挖掘·数据分析
数据皮皮侠5 小时前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链
计算机毕设-小月哥7 小时前
完整源码+技术文档!基于Hadoop+Spark的鲍鱼生理特征大数据分析系统免费分享
大数据·hadoop·spark·numpy·pandas·计算机毕业设计
Jinkxs7 小时前
AI重塑金融风控:从传统规则到智能模型的信贷审批转型案例
大数据·人工智能
hqwest7 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple7 小时前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
时序数据说14 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
2501_9301040418 小时前
GitCode 疑难问题诊疗:全方位指南
大数据·elasticsearch·gitcode