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

新建消息事件

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

c# 复制代码
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<MessageModel>
    {
    }
}

注册、发送提示消息

c# 复制代码
/// <summary>
/// 注册提示消息 
/// </summary>
/// <param name="aggregator"></param>
/// <param name="action"></param>
public static void ResgiterMessage(this IEventAggregator aggregator,
                                   Action<MessageModel> action, string filterName = "Main")
{
    aggregator.GetEvent<MessageEvent>().Subscribe(action,
                                                  ThreadOption.PublisherThread, true, (m) =>
                                                  {
                                                      return m.Filter.Equals(filterName);
                                                  });
}

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

UI增加Snackbar

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

xaml 复制代码
</materialDesign:DrawerHost.LeftDrawerContent>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <materialDesign:Snackbar
                             x:Name="skbar"
                             Panel.ZIndex="1"
                             MessageQueue="{materialDesign:MessageQueue}" />
    <materialDesign:ColorZone

注册消息

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

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

c# 复制代码
aggregator.ResgiterMessage(arg =>
                           {
                               this.skbar.MessageQueue.Enqueue(arg.Message);
                           });

在需要的地方推送消息

c# 复制代码
aggregator.SendMessage("已完成!");
相关推荐
tingshuo29173 小时前
D006 【模板】并查集
笔记
tingshuo29171 天前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
Scout-leaf4 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
西岸行者6 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky6 天前
Django入门笔记
笔记·django
勇气要爆发6 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意6 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发6 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
别催小唐敲代码6 天前
嵌入式学习路线
学习
qianshanxue116 天前
计算机操作的一些笔记标题
笔记