2015-04-15 58 views
1

我有一個WPF應用程序,它由長時間運行的主窗口組成。 在MainWindow構造函數中,定義並初始化我的notifyIcon,如下所示。Windows窗體NotifyIcon在WPF應用程序中隨機消散

notifyIcon = new System.Windows.Forms.NotifyIcon(); 
       if (File.Exists(logoFile)) 
       { 
        BitmapImage image = new BitmapImage(new Uri(logoFile, UriKind.Absolute)); 
        this.Icon = image; 
        notifyIcon.Icon = new System.Drawing.Icon(logoFile); 
        this.gridAbout_imgLogo.Source = image; 
       } 
       else 
       { 
        using (Stream iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/APP;component/Resources/Logo.ico")).Stream) 
        { 
         System.Drawing.Icon defaultIcon = new System.Drawing.Icon(iconStream); 
         notifyIcon.Icon = defaultIcon; 
         BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/APP;component/Resources/Logo.ico")); 
         this.Icon = image; 
         this.gridAbout_imgLogo.Source = image; 
        } 
       } 
       if (config != null) 
        notifyIcon.Text = config.app_name; 
       else 
        notifyIcon.Text = "APP"; 
       notifyIcon.DoubleClick += notifyIcon_DoubleClick; 
       notifyIcon.Visible = true; 
       System.Windows.Forms.ContextMenu m = new System.Windows.Forms.ContextMenu(); 
       System.Windows.Forms.MenuItem mi = new System.Windows.Forms.MenuItem(); 
       mi.Text = "Show App Status"; 
       mi.Click += (s, e) => ShowApplication(); 
       m.MenuItems.Add(mi); 

# if DEBUG 
       System.Windows.Forms.MenuItem mi1 = new System.Windows.Forms.MenuItem(); 
       mi1.Text = "Exit"; 
       mi1.Click += (s, e) => Application.Current.Shutdown(); 
       m.MenuItems.Add(mi1); 
# endif 

       notifyIcon.ContextMenu = m; 

有時通知圖標從托盤中消失。沒有代碼,如nofityIcon.Visible = false。 當我檢查任務管理器時,我可以看到我的應用程序正在運行。

是否有其他原因片狀NotifyIcon行爲,以及補救措施?

回答

1

請檢查系統托盤中的通知圖標設置。 ControlPanel->通知區域圖標 - >選中「始終顯示所有圖標」複選框(Windows 7)。

+0

是的,我檢查了所有的圖標。該圖標丟失。 –

+0

1.圖標是否在啓動應用程序時出現。 2.你能告訴我什麼動作使圖標不可見,雙擊,鼠標右鍵點擊嗎? 3. logoFile是否存在? – lerner1225

+0

在應用程序開始時會出現athe圖標。我無法弄清楚它何時消失但隨機消失。是logofile存在。從另一篇文章中,我瞭解到,與UI不同的一個不同的線程的notifyicon初始化會導致類似的錯誤。不幸的是,那也是我的情況。現在,我改變了邏輯,以便它僅在主UI線程上初始化。讓我們看看它是怎麼回事! –

相關問題