WPF实战学习笔记27-全局通知

新建消息事件

添加文件:Mytodo.Common.Events.MessageModel.cs

using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Diagnostics;

namespace Mytodo.Common.Events
{
    public class MessageModel
    {
        public string Filter { get; set; }
        public string Message { get; set; }
    }
    public class MessageEvent:PubSubEvent
    {
    }
}

注册、发送提示消息

/// 
/// 注册提示消息 
/// 
/// 
/// 
public static void ResgiterMessage(this IEventAggregator aggregator,
                                   Action action, string filterName = "Main")
{
    aggregator.GetEvent().Subscribe(action,
                                                  ThreadOption.PublisherThread, true, (m) =>
                                                  {
                                                      return m.Filter.Equals(filterName);
                                                  });
}

/// 
/// 发送提示消息
/// 
/// 
/// 
public static void SendMessage(this IEventAggregator aggregator, string message, string filterName = "Main")
{
    aggregator.GetEvent().Publish(new MessageModel()
                                                {
                                                    Filter = filterName,
                                                    Message = message,
                                                });
}

UI增加Snackbar

修改文件:Mytodo.Views.MainView.xaml



    
        
        
    
    
    

注册消息

修改文件:Mytodo.Views.MainViewcs 构造函数添加

要注意的是,我们要发送的是文本,所以,this.skbar.MessageQueue.Enqueue函数内发送的是文本。

aggregator.ResgiterMessage(arg =>
                           {
                               this.skbar.MessageQueue.Enqueue(arg.Message);
                           });

在需要的地方推送消息

aggregator.SendMessage("已完成!");

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)