我找到了一个不太明显的解决方案,但可以说更适合 MVVM 模式。它允许在 XAML 中创建 TaskbarIcon 并由 ViewModel 提供新的气球数据。
首先要做的是添加某种功能来通知 TaskbarIcon 我们希望它显示一个气泡我选择使用 Microsofts Rx Extensions (NuGet Package Rx-Main) 来实现此目的,但任何合适的基础设施都可以工作。这是新的 TaskbarIcon 类,并且是一个用于保存我们希望在调用 ShowBubbleTip 方法时传递的数据的类。
using Hardcodet.Wpf.TaskbarNotification;
using System;
using System.Windows;
namespace Phi.Utility
{
public class TaskbarIconRxBallonNotification
{
public Hardcodet.Wpf.TaskbarNotification.BalloonIcon Icon
{
get;
private set;
}
public string BallonTitle
{
get;
private set;
}
public string Message
{
get;
private set;
}
public TaskbarIconRxBallonNotification(Hardcodet.Wpf.TaskbarNotification.BalloonIcon icon, string balloonTitle, string message)
{
Icon = icon;
BallonTitle = balloonTitle;
Message = message;
}
}
public class TaskbarIconRx : TaskbarIcon
{
public IObservable<TaskbarIconRxBallonNotification> BalloonTipNotifier
{
get { return (IObservable<TaskbarIconRxBallonNotification>)GetValue(BallonTipNotifierProperty); }
set { SetValue(BallonTipNotifierProperty, value); }
}
// Using a DependencyProperty as the backing store for BalloonSubject. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BallonTipNotifierProperty =
DependencyProperty.Register("BalloonTipNotifier", typeof(IObservable<TaskbarIconRxBallonNotification>), typeof(TaskbarIconRx), new PropertyMetadata(null, BallonTipNotifierChanged));
//What to do when we get a new ballonIcon request
protected void OnNextNotification(TaskbarIconRxBallonNotification notification)
{
ShowBalloonTip("", notification.Message, BalloonIcon.Info);
}
private IDisposable _subscription;
//Make sure swapping out bindings doesn't break our program.
public static void BallonTipNotifierChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TaskbarIconRx currentNotifier = d as TaskbarIconRx;
if (currentNotifier != null)
{
IObservable<TaskbarIconRxBallonNotification> prev = e.OldValue as IObservable<TaskbarIconRxBallonNotification>;
IObservable<TaskbarIconRxBallonNotification> next = e.NewValue as IObservable<TaskbarIconRxBallonNotification>;
if (currentNotifier._subscription != null)
{
currentNotifier._subscription.Dispose();
currentNotifier._subscription = null;
}
if ((next != null))
{
currentNotifier._subscription = next.Subscribe(currentNotifier.OnNextNotification);
}
}
}
}
}
在我们的模型中,我们提供了一个 ISubject 来绑定
using System.Reactive.Subjects;
namespace Phi.Models {
public class SomeModel:ModelBase {
public ISubject<Utility.TaskbarIconRxBallonNotification> NotifierInterface
{
get;
private set;
}
public SomeModel() {
NotifierInterface = new Subject<Utility.TaskbarIconRxBallonNotification>();
}
}
}
在我们的 ViewModel 中,我们现在可以通过模型主题推送通知,如下所示:
namespace Phi.ViewModels{
public class SomeViewModel:ViewModelBase
{
public SomeModel Model{
get;
private set;
}
public void PushNotification(string message)
{
//Pushes a new notification to the TaskbarIcon.
Model.NotifierInterface.OnNext(new Utility.TaskbarIconRxBallonNotification(Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Info, "Title", message));
}
}
}
在 XAML 中,我们绑定到我们的模型 ISubject
<Utility:TaskbarIconRx Visibility= IconSource="/Resources/TinyLogo.ico" BalloonTipNotifier="{Binding Model.NotifierInterface}" >