Prism模块之间通信的几种方式

82 篇文章 12 订阅
53 篇文章 5 订阅

在开发大型复杂系统时,我们通常会按功能将系统分成很多模块,这样模块就可以独立的并行开发、测试、部署、修改。使用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服务。

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里我给您提供一个简单的通信模块的实现,使用了 Java 语言和 Socket 编程。 1. 服务端代码: ```java import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { // 创建 ServerSocket 对象并绑定端口号 serverSocket = new ServerSocket(8888); } catch (IOException e) { System.err.println("Could not listen on port: 8888."); System.exit(-1); } Socket clientSocket = null; System.out.println("等待客户端连接..."); try { // 等待客户端连接 clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(-1); } System.out.println("客户端已连接!"); // 获取输入流和输出流 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); // 读取客户端发送的消息并回复 String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("客户端: " + inputLine); out.println("服务端已接收到消息: " + inputLine); if (inputLine.equals("exit")) break; } out.close(); in.close(); clientSocket.close(); serverSocket.close(); } } ``` 2. 客户端代码: ```java import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { Socket socket = null; PrintWriter out = null; BufferedReader in = null; try { // 创建 Socket 对象并连接服务器 socket = new Socket("localhost", 8888); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: localhost."); System.exit(1); } BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String userInput; // 发送消息到服务器并读取回复 while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("服务端: " + in.readLine()); if (userInput.equals("exit")) break; } out.close(); in.close(); stdIn.close(); socket.close(); } } ``` 这个通信模块的实现比较简单,它只能在本地运行,并且没有进行错误处理和安全性考虑。但是,它可以作为一个基础的 Socket 编程示例,帮助您更好地理解通信模块的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值