2016-12-19 49 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>