用户通知 MVVM

User notification MVVM

我正在尝试创建用户通知。理想情况下,类似吐司的通知会在角落显示大约三秒钟。

我正在使用 MVVM-light,我认为可以使用它的信使服务来完成通知。

我有这个class:

public class NotificationSync
    {
        public string Messages { get; set; }
    }

在一个视图模型中,我这样设置 Messenger:

 Messenger.Default.Send(new NotificationSync()
    {
       Messages = "message"
    });

在我的 MainviewModel(它是视图的数据上下文)中,我是这样听的:

  Messenger.Default.Register<NotificationSync>(this, (action) =>
               Mess = action.Messages );

Mess 是视图模型上的字符串 属性:

   private string mess;
    public string Mess
    {
        get { return mess; }
        set
        {
            mess = value;
            RaisePropertyChanged("Mess");
        }
    }

我想对 mess 做的是以类似 toast 的方式将它绑定到我的视图。 I.E 在我看来显示它几秒钟。有关如何执行此操作的任何提示?谢谢。

你的 toast 的可见度 属性 加上计时器怎么样?

Messenger.Default.Register<NotificationSync>(this, (action) =>
               Mess = action.Messages 
               ShowToast();
);

private void ShowToast()
{
    IsToastVisible = true;
    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
    dispatcherTimer.Start();
}

void OnTimerTick(Object sender, EventArgs args)
{
    IsToastVisible = false;
}

这假定 Mess 绑定到的文本框也绑定到 IsToastVisible 并且它使用 VisibilityConverter。