2016-12-19 46 views
-1

我使用C#(WPF)和MVVM模式編寫應用程序。該應用程序允許在本地網絡的客戶端之間發送通知。下面的設計實例描述了我曾說過:使用MVVM模式更改系統托盤圖標(C#/ WPF)

enter image description here

當客戶端應用程序最小化它把它在系統托盤中。

通知系統發送正常。

我的問題是:當客戶端2的應用程序被最小化和客戶機1發送一個通知:如何更改客戶端2的系統托盤圖標以通知新通知已被接收(使​​用MVVM模式)?

在此先感謝

更新:

用於創建通知試圖標的代碼是:

MainWindow.xaml.cs

using ControlPanelNetClient.ViewModel; 
using System; 
using System.Windows; 
using System.Windows.Forms; 

namespace ControlPanelNetClient.View 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     readonly ViewModelControlPanel _vm; 

     private NotifyIcon m_notifyIcon; 
     private WindowState m_storedWindowState = WindowState.Maximized; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      _vm = new ViewModelControlPanel(); 
      base.DataContext = _vm; 

      m_notifyIcon = new System.Windows.Forms.NotifyIcon(); 
      m_notifyIcon.BalloonTipText = "Click to open."; 
      m_notifyIcon.BalloonTipTitle = "KM Control Panel"; 
      m_notifyIcon.Text = "KM Control Panel"; 
      m_notifyIcon.Icon = new System.Drawing.Icon("favicon.ico"); 
      m_notifyIcon.Click += new EventHandler(NotifyIcon_Click);    

      this.Closed += new EventHandler(MainWindow_Closed); 
      this.StateChanged += new EventHandler(MainWindow_StateChanged); 
     } 

     private void MainWindow_IsVisibleChanged(object sender, EventArgs e) 
     { 
      CheckTrayIcon(); 
     } 

     void MainWindow_Closed(object sender, EventArgs e) 
     { 
      _vm.StopListeningThread(); 
      m_notifyIcon.Dispose(); 
      m_notifyIcon = null; 
     } 

     void MainWindow_StateChanged(object sender, EventArgs args) 
     { 
      if (WindowState == WindowState.Minimized) 
      { 
       Hide(); 
       if (m_notifyIcon != null) 
       { 
        m_notifyIcon.ShowBalloonTip(1000); 
       }      
      } 
      else 
      { 
       m_storedWindowState = WindowState; 
      }     
     } 

     void MainWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args) 
     { 
      CheckTrayIcon(); 
     } 

     void NotifyIcon_Click(object sender, EventArgs e) 
     { 
      Show(); 
      WindowState = m_storedWindowState; 
     } 

     void CheckTrayIcon() 
     { 
      ShowTrayIcon(!IsVisible); 
     } 

     void ShowTrayIcon(bool show) 
     { 
      if (m_notifyIcon != null) 
      { 
       m_notifyIcon.Visible = show; 
      }     
     } 
    } 
} 
+0

可以共享至今代碼托盤圖標你試過嗎? btw我不是downvoter :) – WPFUser

+0

你使用什麼樣的插件使用托盤?你寫了你自己的?如果是這樣,告訴我們。 – FCin

+0

您更改NotifyIcon.Icon。這意味着無論誰構建NotifyIcon的(可能在app.cs或mainwindow.cs)具有用於客戶機2保持既它和所述TCP客戶端的引用(或它的管理型)當一個消息到來時,TCP客戶端通知那些對申請的新狀態感興趣,並且相應地持有這兩項更新。這就是所謂的編碼 - 填充尚不存在的零件。 – Will

回答

1

您正在使用系統。 Windows.Forms.NotifyIcon。如果您想使用MVVM更改圖標,我認爲我們沒有簡單的解決方案。 在WPF托盤圖標,你可以看看這個http://www.hardcodet.net/wpf-notifyicon 下面是一個示例代碼來創建使用該庫

<tb:TaskbarIcon x:Key="NotifyIcon"    
         IconSource="{Binding IconPath}" 
         ToolTipText="{Binding Tooltip}" 
         ContextMenu="{StaticResource SysTrayMenu}" 
         DoubleClickCommand="{Binding ManageCommand}"> 
     </tb:TaskbarIcon>