如何使用WPF NotifyIcon

如何使用WPF NotifyIcon,notifyicon,Notifyicon,我尝试使用WPF通知图标。 我创建一个新的wpf项目,从示例项目导入dll,然后复制xaml部分和代码。 XAML: C#代码: 使用Hardcodet.Wpf.TaskbarNotification; 使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows

我尝试使用WPF通知图标。 我创建一个新的wpf项目,从示例项目导入dll,然后复制xaml部分和代码。 XAML:


C#代码:

使用Hardcodet.Wpf.TaskbarNotification;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间trayicon
{
/// 
///每个MainWindow.xaml的内部逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
//注意:除了最简单的场景外,所有场景都建议使用XAML
TaskbarIcon tbi=新TaskbarIcon();
tbi.Icon=Resources.Error;
tbi.ToolTipText=“你好世界”;
}
}
}
第tbi.Icon=Resources.error行中有错误; 如果我对该行进行注释,我会在ToolTipText=“hello world”行中获得一个XamlParseException


你能帮忙吗?谢谢

首先,你列举了两个非战犯。一个在XAML中,一个在代码隐藏中。没必要那样。只留下XAML一个

关于IconSource的错误是VS找不到“error.ico”。您必须将其添加到项目中,并将编译设置为“资源”(右键单击Error.ico,转到properties并在那里进行更改)

至于第二个错误(当您注释掉时):您不能注释掉XAML中控件的属性,因为它将抛出parse erros

<Window x:Class="trayicon.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tb="http://www.hardcodet.net/taskbar"
    Title="MainWindow" Height="350" Width="525">
<Grid>

<!--
  in order to create a NotifyIcon, all you need is the
  namespace declaration (see above on line 4) and a simple
  declaration
-->
    <tb:TaskbarIcon
  IconSource="Error.ico"
  ToolTipText="hello world"
    />
</Grid>
</Window>
using Hardcodet.Wpf.TaskbarNotification;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace trayicon
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Note: XAML is suggested for all but the simplest scenarios
            TaskbarIcon tbi = new TaskbarIcon();
            tbi.Icon = Resources.Error;
            tbi.ToolTipText = "hello world";
        }
    }
}