使用MVVM模式(C#/ WPF)更改系统托盘图标

时间:2016-12-19 08:47:01

标签: c# wpf mvvm

我正在使用C#(WPF)和MVVM模式编写应用程序。该应用程序允许在本地网络的客户端之间发送通知。以下设计实例描述了我之前所说的内容:

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

1 个答案:

答案 0 :(得分:1)

您正在使用System.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>