mvvm - 如何在MVVM项目中实现notifyicon

标签 mvvm notifyicon

我正在尝试在 MVVM 项目中实现通知图标 ( http://www.hardcodet.net/projects/wpf-notifyicon )。我了解此控件旨在用于常规 WPF 项目。

我想知道如何实现气球功能( Balloon feature )。 正如本教程中指定的,需要调用方法“ShowBallonTip”

//show balloon with built-in icon
  MyNotifyIcon.ShowBalloonTip(title, text, BalloonIcon.Error);

我能想到的唯一可以调用此方法的地方是在后面的代码中。我在 View 代码后面有一些代码没有问题(即使我不想有任何代码),但我不知道如何让 View 模型与 View 对话并要求它调用它方法。

即使我将此方法放入事件中,如何从 View 模型中以编程方式引发此事件?

知道如何实现这一目标吗?

最佳答案

我已经能够通过初始化 View 模型中的图标而不是 XAML 来显示气球提示。

只需在我的命令中调用 ShowBalloonTip 方法即可实现此目的。

我为通知图标创建了一个包装器: 通知服务:

public class NotifyService : INotifyService
{
    private TaskbarIcon icon = new TaskbarIcon
        {
            Name = "NotifyIcon",
            Icon =
                new System.Drawing.Icon(
                    Application.GetResourceStream(Utils.FileUtils.MakeUri("/Icons/email.ico")).Stream),
        };


    public void Notify(string message)
    {

        icon.ShowBalloonTip("title", message, BalloonIcon.None);
    }

    public void ChangeIconSource(string path)
    {
        icon.Icon = new System.Drawing.Icon(
                    Application.GetResourceStream(Utils.FileUtils.MakeUri(path)).Stream);
    }
}

我在我的 View 模型中使用了它: View 模型

public class MainWindowViewModel : WindowViewModelBase
{
    private readonly INotifyService notifyService = new NotifyService();

    #region Fields
    private static HomeWindowViewModel homeViewModel = new HomeWindowViewModel();
    #endregion
    /// Initializes a new instance of the <see cref="MainWindowViewModel"/> class.
    /// </summary>
    public MainWindowViewModel()
        : base()
    {
        CurrentViewModel = homeViewModel;
    }

    #region Methods

    protected override void OnViewModelPropertyChanged(IViewModel viewModel, string propertyName)
    {
        int t = 2;
    }

    protected override void OnViewModelCommandExecuted(IViewModel viewModel, ICatelCommand command, object commandParameter)
    {
        int t = 2;
        notifyService.ChangeIconSource(@"/Icons/new_email.ico");
        notifyService.Notify("test");
    }
    #endregion
}

关于mvvm - 如何在MVVM项目中实现notifyicon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501348/

相关文章:

vb.net - 在运行时更改 NotifyIcon 图标

c# - 在 DataGrid 上绑定(bind) View 和 ViewModel

javascript - Knockout JS 入门

c# - WPF MVVM : How to close a window

c# - 如何将 WPF NotifyIcon 与 Caliburn.Micro 集成

powershell - PowerShell,当 “Wait-Event”触发时从 “Register-ObjectEvent -Action”返回吗?

android - 如何编写一个使用 Context 从 Android 框架中检索数据的用例

c# - 抽象 ViewModel 在被继承时是否被视为模型?

c# - 通知图标不显示

c# - NotifyIcon 显示来自程序的消息