zoukankan      html  css  js  c++  java
  • Prism学习笔记-模块之间通信的几种方式

    在开发大型复杂系统时,我们通常会按功能将系统分成很多模块,这样模块就可以独立的并行开发、测试、部署、修改。使用Prism框架设计表现层时,我们也会遵循这个原则,按功能相关性将界面划分为多个模块,每个模块又包含多个Region。这就需要解决模块之间、Region之间经常需要进行通信的问题,Prism提供了以下几种方式:

    1、聚合事件(Event aggregation

    使用方式,先在一个公共模块中定义一个事件MyEvent ,传输数据为MyEntity。

    public class MyEvent : CompositePresentationEvent<MyEntity>
    {
    }

    然后在需要等待处理事件的模块中订阅事件,如下所示:

    private IEventAggregator eventAggregator;

    eventAggregator = (IEventAggregator)ServiceLocator.Current.GetService(typeof(IEventAggregator));

    eventAggregator.GetEvent<MyEvent>().Subscribe(MyEventHandler, true);

    public void MyEventHandler(MyEntity myEntity)
    {

    }

    eventAggregator 相当于一个全局的集合,保存了所有订阅的事件。

    在发起通信的模块中发布事件,如下所示:

    eventAggregator.GetEvent<CreatePlanEvent>().Publish(new MyEntity());

    2、全局命令

    使用方式,在公共模块中定义一个全局命令类:

    public static class GlobalCommands
    {
            public static CompositeCommand OpenCommand = new CompositeCommand();

    }

    在使用该命令的View中:

    <Button Command="{x:Static s:GlobalCommands.OpenCommand }" >

    在响应该命令的模块中:

    public ICommand OpenCommand { get; set; }

    OpenCommand = new RelayCommand(param => this.Open(param));

    GlobalCommands.OpenCommand.RegisterCommand(OpenCommand);

    不用绑定时可以这样执行:

    GlobalCommands.OpenCommand.Execute(param);

    3、Region context

    在Prism安装文件中自带的UIComposition例子中演示了两个Tab页共享一个数据集,

    用法是在View中:

    prism:RegionManager.RegionContext="{Binding CurrentEmployee}"

    在两个Tab页的View的后台代码中:

    RegionContext.GetObservableContext(this).PropertyChanged += (s, e)
                                                                            =>
                                                                            employeeDetailsViewModel.CurrentEmployee =
                                                                            RegionContext.GetObservableContext(this).Value
                                                                            as Employee;

    我们还可以利用Region.Context属性共享数据,Region.Context中可以保存任何该Region需要与其他Region共享的数据。

    因此我在实际使用时用Region.Context来保存一个控件对象的引用。如下所示:

    在一个模块中保存

    mainRegion.Context = new ContentControl();

    在另一个模块中取出

    IRegion mainRegion = regionManager.Regions["MainRegion"];
    if (mainRegion == null) return;
    ContentControlpane = mainRegion.Context as ContentControl;

    因为regionManager是全局的,所以可以随时获得感兴趣的Region和Context

    4、共享服务

    这种方法我们在前面已经接触到了,如:

    eventAggregator = (IEventAggregator)ServiceLocator.Current.GetService(typeof(IEventAggregator));

    还可以,container= (IEventAggregator)ServiceLocator.Current.GetService(typeof(IUnityContainer));

    IEventAggregator和IUnityContainer是Prism已有的服务,我们还可以自定义服务,请参考Prism自带的StockTrader RI例子

    中的IMarketHistoryService服务。

  • 相关阅读:
    Win10 下使用 ionic 框架开发 android 应用之搭载开发环境
    C# 图片压缩
    [安卓应用发布]HOLO一键锁屏
    SRS中增加身份验证
    实际搭建SRS测试的初步问题总结
    推荐一款优秀的开源流媒体服务器系统:SRS(Simple RTMP Server)
    C#--格式化数值数据
    操作系统学习笔记系列(二)-操作系统结构
    操作系统学习笔记系列(一)- 导论
    C语言文件读写
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/5013144.html
Copyright © 2011-2022 走看看