商业网站建设案例,html入门视频教程,python 网站建设,温州网页制作文章目录 前言代码逻辑修改 总结 前言
我们需要一个全局事件订阅发布功能#xff0c;实现页面通讯。使两个毫无关系的页面通过一个中间量进行通讯。
代码
IEventAggregator#xff1a;消息订阅集合
这个是Prism提供的消息订阅功能。使用如下
设置订阅类型#xff0c;即… 文章目录 前言代码逻辑修改 总结 前言
我们需要一个全局事件订阅发布功能实现页面通讯。使两个毫无关系的页面通过一个中间量进行通讯。
代码
IEventAggregator消息订阅集合
这个是Prism提供的消息订阅功能。使用如下
设置订阅类型即关键字
PubSubEvent里面存放的是订阅的数据类型推荐使用元祖
///
public class EventClass : PubSubEventstring
{}官方案例在ViewModel中使用 private readonly IEventAggregator eventAggregator;public string MyTitle { get; set; }public ViewAViewModel(IEventAggregator eventAggregator){//通过Prism注入得到this.eventAggregator eventAggregator;//订阅eventAggregator.GetEventEventClass().Subscribe(res {Debug.WriteLine(res.ToString());});//推送eventAggregator.GetEventEventClass().Publish(我是侧边栏传来的值事件通知);}
Tips:订阅中传递的值和EventClass: PubSubEventstring 中设置的类型有关。
但是经过我的测试好像不能跨页面进行通讯。
逻辑修改 public partial class App{//在App.xaml中进行声明public static IEventAggregator EventAggregator { get;set; } new EventAggregator();}对应每个xxxView页面都定义一个xxxViewEvent。
在每个ViewModel的构造函数中订阅自己的ViewEvent这样别的页面直接对该Event进行推送即可达到路由传值。通过key,value的形式对返回值进行解析。
演示事例
public ViewAViewModel(IEventAggregator eventAggregator){App.EventAggregator.GetEventViewAEvent().Publish(我是ViewModel传来的值);App.EventAggregator.GetEventViewAEvent().Subscribe(res {Debug.WriteLine(res.ToString());MyTitle res;});}总结
Prism和WPF给了我们很多的解决方案但是其实我们只需要最简单又好用的解决方案即可。有了页面通讯完全不需要路由通讯了。直接在页面跳转完成之后再页面通讯即可。统一而又优雅的解决方案才是我们需要的。