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

相关推荐
Elastic 中国社区官方博客2 分钟前
Elasticsearch 字符串包含子字符串:高级查询技巧
大数据·数据库·elasticsearch·搜索引擎·全文检索·lucene
张先shen1 小时前
Elasticsearch RESTful API入门:全文搜索实战
java·大数据·elasticsearch·搜索引擎·全文检索·restful
天翼云开发者社区1 小时前
Doris-HDFS LOAD常见问题汇总(二)
大数据·doris
简婷187019987752 小时前
源网荷储 + 零碳园区:一场关于能源与未来的双向奔赴
大数据·人工智能·能源
2201_756776772 小时前
网络安全初级
大数据·elasticsearch·搜索引擎
m0_703323672 小时前
SEO外包服务甄选指南:避开陷阱,精准匹配
大数据·人工智能
zzywxc7873 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt
张先shen3 小时前
Elasticsearch RESTful API入门:索引的增删改查完全指南
java·大数据·elasticsearch·搜索引擎·架构·全文检索·restful
yzx9910133 小时前
AI大模型平台
大数据·人工智能·深度学习·机器学习
189228048616 小时前
NW710NW713美光固态闪存NW719NW720
大数据·服务器·网络·人工智能·科技