首页 > 其他分享 >WPF使用事件聚合器,实现任意页面跨页通信

WPF使用事件聚合器,实现任意页面跨页通信

时间:2024-05-29 22:23:29浏览次数:14  
标签:里面 对话框 跨页 消息 WPF 接收 message 页面

前言:最近几天有好几个小伙伴玩WPF,遇到不同页面,不知道要怎么传递消息。于是,我今天就来演示一个事件聚合器的玩法,采用prism框架来实现。作为福利,内容附带了主页面打开对话框时候直接通过参数传递消息的一个小例子,具体请自行围观。

以下内容,创建wpf项目以及引用prism和实现依赖注入等细节,可以参考我的其他文章。因为本文章使用该链接的教程的原始demo直接进行添加:

https://mp.weixin.qq.com/s/DHKr68cIMWu0tOrO4RBjXA

在以上原始demo的基础上(知道prism框架的引用、MVVM的实现,可以无视上面文章教程),我在主页新建一个按钮,用来打开对话框使用。

0   定义一个消息传递的类和事件。PubSubEvent是消息的发布和订阅事件,接收可自定义消息的数据,例如我自定义一个测试消息类型。使用默认类型也可以,例如bool、string等等。 0   主页面里面,注入IEventAggregator服务接口,然后在构造函数里面直接定义消息接收方法GetMessage,该方法用来接收其他页面传递的消息,并在debug控制台输出结果。 0   创建一个用户控件,并提供一个输入框和一个按钮,用来做测试发送消息使用。 0 对应的ViewModel里面,需要继承 IDialogAware,用来代表他是一个弹出对话框(你是页面也行,问题不大,对话框的目的是为了接收主页面传递数据给对话弹出框)。然后在对话框打开方法里面,假设接收一个参数为“message”的string类型数据。 0 对话框里面新增输入框的数据绑定属性,用于等下对话框打开以后,主页面传递的消息参数可以马上被对话框获取到并显示到页面的输入框里面,当做验证。 0 主页面的按钮,用来弹出对话框,并且传入一个message参数。res可以用来接收返回值,此处不再演示,原理一样。 0 弹出框关闭方法里面也可以新增回传的消息,用法和打开弹出框雷同,感兴趣可以自行尝试。 在App.xaml.cs里面注册对话框 0 启动程序,先测试一下弹出框打开效果。可以发现,弹出框被打开,并且输入框里面的数据就是打开弹出框时候,主页面传入的数据。 0 在对话框的业务代码里面,按钮的事件里面新增一个消息发布内容,用来把消息(输入框输入的内容和当前时间)发布给TestMessageEvent事件,只要其他地方有订阅该事件,均可收到该发布的消息数据。 0 前面主页面已经写好了消息接收的方法。现在运行看一下效果:   0 再发送一个其他内容,例如 公众号:【Dotnet Dancer】,同样的,也可以成功被接收到。这个就是跨页面通信的使用方式,不管你的页面是父级、子级、爷孙级别、甚至毫无关联的页面,只要在同一个程序里面,都可以这样互相传递消息。 0

以上内容核心代码:

1、打开对话框

private void ExecuteOpenDialog()
{
    DialogParameters pairs = new DialogParameters();
    pairs.Add("message", "这是一个测试消息");
    _dialogService.ShowDialog("TestDialog", pairs, (res) => {});
}

 

2、消息发布与接收:

//发布

 private void ExcuteSendMessage (){
     TestMessage message = new TestMessage();
     message.Now = DateTime.Now;
     message.Message = this.Message;
     _aggregator.GetEvent<TestMessageEvent>().Publish(message);
 }
 
//接收
_aggregator.GetEvent<TestMessageEvent>().Subscribe(GetMessage);
 private void GetMessage(TestMessage message)
 {
     Debug.WriteLine($"{message.Now}>>>>>>{message.Message}");
 }

 

3、对话框参数接收:

public void OnDialogOpened(IDialogParameters parameters)
 {
     string message = parameters.GetValue<string>("message");
     this.Message = message;
 }
 

以上就是该文章的全部内容,如果需要以上demo源码,可以在我个人公众号【Dotnet Dancer】后台回复"聚合器”即可获取demo源码下载链接。

 

 

标签:里面,对话框,跨页,消息,WPF,接收,message,页面
From: https://www.cnblogs.com/weskynet/p/18221240

相关文章

  • HarmonyOS实战开发:@ohos.router (页面路由)
    本模块提供通过不同的url访问不同的页面,包括跳转到应用内的指定页面、同应用内的某个页面替换当前页面、返回上一页面或指定的页面等。说明本模块首批接口从APIversion8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。页面路由需要在页面渲染完成之后......
  • 我的 Django 注册页面重定向不起作用
    我正试图为我的Django项目制作一个注册页面。当我编译注册表单时,它出现了图片中的错误,因为它试图转到以下路径(hedoublesusers/register):localhost/users/register/users/register/。我的代码如下:......
  • WPF Image enlarge via MouseWheel, selected image center does not shift
    //xaml<Windowx:Class="WpfApp123.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • wpf 捕获全局异常
    App(){this.Startup+=App_Startup;}privatevoidApp_Startup(objectsender,StartupEventArgse){this.DispatcherUnhandledException+=App_DispatcherUnhandledException;AppDomain.CurrentDomain.UnhandledException+=CurrentD......
  • .net 8 WPF发布程序只生成exe
    右击项目选择发布配置发布信息修改FolderProfile.pubxml文件内容ReleaseAnyCPUbin\Release\net8.0-windows\publish\win-x64</PublishDir>FileSystem<_TargetId>Folder</_TargetId>net8.0-windowswin-x64truefalsetruetrue<_SuppressWpfTrimError>......
  • vue前端页面搭建
    十、页面搭建学习10.1安装element在这里看一下有没有elementui,有就是下载成功了。10.2mainjs全局引入importElementUIfrom'element-ui';import'element-ui/lib/theme-chalk/index.css';Vue.use(ElementUI)10.3简单试用运行serve10.4页面布局(都可以直接查......
  • WPF Image ZoomIn ZoomOut pan
    <Windowx:Class="WpfApp120.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Path Geometry PathFigureCollection PathFigure PathFigure.Segments PolyQuadra
    <Windowx:Class="WpfApp118.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • hexo + vercel 构建你的静态页面博客系统(免费)
    参考资料注册vercel、安装hexo:https://blog.chitang.dev/posts/hexo-vercel-blog/插入图片:https://blog.csdn.net/m0_43401436/article/details/107191688QuickStart如果一切都安装完毕了,命令行下进入hexo工作目录新建博文的命令hexonewbravo你会在source/_post......
  • WPF DrawingContext DrawingVisual OnRender()
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;......