Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MVVM模式(C/WPF)更改系统托盘图标_C#_Wpf_Mvvm - Fatal编程技术网

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

C# 使用MVVM模式(C/WPF)更改系统托盘图标,c#,wpf,mvvm,C#,Wpf,Mvvm,我正在使用C WPF和MVVM模式编程一个应用程序。该应用程序允许在本地网络的客户端之间发送通知。下面的设计示例描述了我之前说过的内容: 当客户端应用程序最小化时,它会将其放在系统托盘中 发送通知的系统运行良好 我的问题是:当客户机2应用程序最小化并且客户机1发送通知时:如何更改客户机2的系统托盘图标以通知已使用MVVM模式接收到新通知 提前谢谢 更新: 创建通知尝试图标的代码为: MainWindow.xaml.cs using ControlPanelNetClient.ViewModel;

我正在使用C WPF和MVVM模式编程一个应用程序。该应用程序允许在本地网络的客户端之间发送通知。下面的设计示例描述了我之前说过的内容:

当客户端应用程序最小化时,它会将其放在系统托盘中

发送通知的系统运行良好

我的问题是:当客户机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;
            }                
        }
    }
}

您正在使用System.Windows.Forms.NotifyIcon。如果您想使用MVVM更改图标,我认为我们没有一个简单的解决方案。 对于WPF中的托盘图标,您可以查看此图标 下面是使用该库创建托盘图标的示例代码

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

你能分享到目前为止你尝试过的代码吗?顺便说一句,我不是下载者:你用什么样的插件来使用托盘?你自己写的吗?如果是,请向我们显示。您更改了NotifyIcon.Icon。这意味着可能在app.cs或mainwindow.cs中构造NotifyIcon的人必须同时持有对它和tcp客户端或其客户端2管理类型的引用。当一条消息出现时,tcp客户机将通知那些感兴趣的人应用程序的新状态,并相应地通知持有这两个更新的人。这被称为编码填充尚不存在的部分。