2014-10-09 46 views
0

我嘗試使用WPF Notify圖標http://www.codeproject.com/Articles/36468/WPF-NotifyIcon。 我創建了一個新的wpf項目,我從示例項目中導入了dll,然後複製了xaml部件和代碼。 XAML:如何使用WPF NotifyIcon

<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 =「hello world」

你能幫忙嗎?謝謝

回答

1

首先,你instanciating 2 notfiyicons。一個在XAML中,一個在代碼後面。這是沒有必要的。只留下XAML一個。

有關IconSource的錯誤是VS無法找到「Error.ico」。您必須將其添加到項目中,並將編譯設置爲「資源」(右鍵單擊Error.ico,轉至屬性並在此處更改)。

至於第二個錯誤(當你註釋掉了):你不能註釋掉XAML中控件的屬性,因爲它會拋出解析錯誤。