I am trying to implement a notifyicon (http://www.hardcodet.net/projects/wpf-notifyicon) in MVVM project. I understand this control is meant to be used in a regular WPF project.
I am wondering how to implement the ballon feature (Balloon feature). As specified in this tutorial the method "ShowBallonTip" needs to be called
//show balloon with built-in icon
MyNotifyIcon.ShowBalloonTip(title, text, BalloonIcon.Error);
The only place I could call this method, I can think of, is in the code behind. I do not have a problem with having a little code in a view code behind (even if I would prefer not having any) but I can not figure out how I can have the view model to talk to the view and asks it to call this method.
Even if I place this method in an event how can I raise this event programatically from the viewmodel?
Any idea how I could achieve this?
I have been able to display a balloon tip by initialising the icon in the viewmodel instead of the XAML.
Just calling the ShowBalloonTip method in my command do the trick.
I created a wrapper for the notify Icon: NotifyService:
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);
}
}
And I used it in my view model: viewmodel
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
}