Prism应用开发(八)——松耦合组件之间通信
2016-08-12 09:15
816 查看
一、Commands
创建一个全局的command,该command将会在各个组件之间共享。
[csharp]
view plain
copy
print?
public static class GlobalCommands
{
public static CompositeCommand MyCompositeCommand = new CompositeCommand();
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
[csharp]
view plain
copy
print?
GlobalCommands.MyCompositeCommand.RegisterCommand(command1);
GlobalCommands.MyCompositeCommand.RegisterCommand(command2);
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
[html]
view plain
copy
print?
<Button Name="MyCompositeCommandButton" Command="{x:Static
local:GlobalCommands.MyCompositeCommand}">Execute My Composite Command </Button>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
二、Shared Service
module之间可以通过Shared Service相互通信而不用直接引用另一个module。
[csharp]
view plain
copy
print?
protected void RegisterViewsAndServices()
{
_container.RegisterType<IMarketFeedService, MarketFeedService>(new
ContainerControlledLifetimeManager());
//...
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
三、Event Aggregation
Prism提供了为松耦合组件之间通信提供了事件的机制。这个机制基于event aggregator service。允许发布和订阅事件而不用使得各个module引用对方。
下面的代码用户创建,发布和订阅事件
[csharp]
view plain
copy
print?
public class TickerSymbolSelectedEvent : CompositePresentationEvent<string>{}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
[csharp]
view plain
copy
print?
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish("STOCK0");
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
[csharp]
view plain
copy
print?
public void Run()
{
...
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Subscribe(ShowNews,
ThreadOption.UIThread);
);
}
public void ShowNews(string companySymbol)
{
this.articlePresentationModel.SetTickerSymbol(companySymbol);
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
subscribe还提供了用于过滤的参数,例如:
[csharp]
view plain
copy
print?
FundAddedEvent fundAddedEvent = this.eventAggregator.GetEvent<FundAddedEvent>();
fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false,
fundOrder => fundOrder.CustomerId == this.customerId);
创建一个全局的command,该command将会在各个组件之间共享。
[csharp]
view plain
copy
print?
public static class GlobalCommands
{
public static CompositeCommand MyCompositeCommand = new CompositeCommand();
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
public static class GlobalCommands { public static CompositeCommand MyCompositeCommand = new CompositeCommand(); }
[csharp]
view plain
copy
print?
GlobalCommands.MyCompositeCommand.RegisterCommand(command1);
GlobalCommands.MyCompositeCommand.RegisterCommand(command2);
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
GlobalCommands.MyCompositeCommand.RegisterCommand(command1); GlobalCommands.MyCompositeCommand.RegisterCommand(command2);
[html]
view plain
copy
print?
<Button Name="MyCompositeCommandButton" Command="{x:Static
local:GlobalCommands.MyCompositeCommand}">Execute My Composite Command </Button>
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
<Button Name="MyCompositeCommandButton" Command="{x:Static local:GlobalCommands.MyCompositeCommand}">Execute My Composite Command </Button>
二、Shared Service
module之间可以通过Shared Service相互通信而不用直接引用另一个module。
[csharp]
view plain
copy
print?
protected void RegisterViewsAndServices()
{
_container.RegisterType<IMarketFeedService, MarketFeedService>(new
ContainerControlledLifetimeManager());
//...
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
protected void RegisterViewsAndServices() { _container.RegisterType<IMarketFeedService, MarketFeedService>(new ContainerControlledLifetimeManager()); //... }
三、Event Aggregation
Prism提供了为松耦合组件之间通信提供了事件的机制。这个机制基于event aggregator service。允许发布和订阅事件而不用使得各个module引用对方。
下面的代码用户创建,发布和订阅事件
[csharp]
view plain
copy
print?
public class TickerSymbolSelectedEvent : CompositePresentationEvent<string>{}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
public class TickerSymbolSelectedEvent : CompositePresentationEvent<string>{}
[csharp]
view plain
copy
print?
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish("STOCK0");
![](https://oscdn.geek-share.com/Uploads/Images/Content/201702/b303364dc8d788bc830b1e2a21d6c01b.png)
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish("STOCK0");
[csharp]
view plain
copy
print?
public void Run()
{
...
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Subscribe(ShowNews,
ThreadOption.UIThread);
);
}
public void ShowNews(string companySymbol)
{
this.articlePresentationModel.SetTickerSymbol(companySymbol);
}
![](https://oscdn.geek-share.com/Uploads/Images/Content/201701/9cc493f1e15b23d0f4eaea0d0f8b35d0.png)
public void Run() { ... this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Subscribe(ShowNews, ThreadOption.UIThread); ); } public void ShowNews(string companySymbol) { this.articlePresentationModel.SetTickerSymbol(companySymbol); }
subscribe还提供了用于过滤的参数,例如:
[csharp]
view plain
copy
print?
FundAddedEvent fundAddedEvent = this.eventAggregator.GetEvent<FundAddedEvent>();
fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false,
fundOrder => fundOrder.CustomerId == this.customerId);
相关文章推荐
- Prism应用开发(八)——松耦合组件之间通信
- EvenBus:Android应用组件之间高效通信的开发库
- Prism 4 文档 ---第9章 松耦合组件之间通信
- iOS开发-应用之间的跳转及通信
- 【Android 应用开发】Activity生命周期 与 Activity 之间的通信
- 【Android 应用开发】Activity生命周期 与 Activity 之间的通信
- react开发教程之React 组件之间的通信方式
- iOS开发-应用之间的跳转及通信
- vue开发:vue2.0父子组件及非父子组件之间的通信方法
- 【Android应用开发技术:应用组件】Handler组件通信机制
- Android应用开发教程:两个运行的Activity之间的通信
- 本文是笔者根据数据库编程经验,利用C++语言的模板、继承、授权、多态等面向对象特性,借鉴命令模式,实现了对象在关系数据中的存储,降低应用系统与数据库之间的耦合,提高开发效率。
- iPhone开发之—守护进程和前台进程之间的通信-前台应用发信息给后台的守护进程
- iOS开发:两个独立应用之间的通信跳转
- iPhone开发之—守护进程和前台进程之间的通信-前台应用发信息给后台的守护进程
- vue开发:vue父子组件与非父子组件之间的通信
- javaMail组件应用---------3 开发邮件系统步骤
- 企业级应用中的Applet和Servlet的通信-Java基础-Java-编程开发
- 服务器应用:实现Linux与宿主机之间的通信
- javaMail组件应用---------3 开发邮件系统步骤