如何使用WPF NotifyIcon

时间:2014-10-09 14:37:12

标签: notifyicon

我尝试使用WPF通知图标http://www.codeproject.com/Articles/36468/WPF-NotifyIcon。 我创建了一个新的wpf项目,我从示例项目中导入dll,然后我复制了xaml部分和代码。 XAML:

<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>

C#代码:

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";
        }
    }
}

行tbi.Icon = Resources.Error中有错误; 如果我评论该行,我在行中获得了一个XamlParseException ToolTipText =&#34; hello world&#34;

你能帮忙吗?感谢

1 个答案:

答案 0 :(得分:1)

首先,你正在实现2个非治疗。一个在XAML中,另一个在代码中。没有必要这样做。只留下XAML。

关于IconSource的错误是VS无法找到&#34; Error.ico&#34;。您必须将它添加到项目中并将编译设置为&#34;资源&#34; (右键单击Error.ico,转到属性并在那里进行更改。)

至于第二个错误(当你注释掉时):你不能在XAML中注释掉控件的属性,因为它会抛出解析错误。