一、Commands
创建一个全局的command,该command将会在各个组件之间共享。
- public static class GlobalCommands
- {
- public static CompositeCommand MyCompositeCommand = new CompositeCommand();
- }
public static class GlobalCommands
{
public static CompositeCommand MyCompositeCommand = new CompositeCommand();
}
- GlobalCommands.MyCompositeCommand.RegisterCommand(command1);
- GlobalCommands.MyCompositeCommand.RegisterCommand(command2);
GlobalCommands.MyCompositeCommand.RegisterCommand(command1);
GlobalCommands.MyCompositeCommand.RegisterCommand(command2);
- <Button Name="MyCompositeCommandButton" Command="{x:Static
- local:GlobalCommands.MyCompositeCommand}">Execute My Composite Command </Button>
<Button Name="MyCompositeCommandButton" Command="{x:Static
local:GlobalCommands.MyCompositeCommand}">Execute My Composite Command </Button>
二、Shared Service
module之间可以通过Shared Service相互通信而不用直接引用另一个module。
- protected void RegisterViewsAndServices()
- {
- _container.RegisterType<IMarketFeedService, MarketFeedService>(new
- ContainerControlledLifetimeManager());
- //...
- }
protected void RegisterViewsAndServices()
{
_container.RegisterType<IMarketFeedService, MarketFeedService>(new
ContainerControlledLifetimeManager());
//...
}
三、Event Aggregation
Prism提供了为松耦合组件之间通信提供了事件的机制。这个机制基于event aggregator service。允许发布和订阅事件而不用使得各个module引用对方。
下面的代码用户创建,发布和订阅事件
public class TickerSymbolSelectedEvent : CompositePresentationEvent<string>{}
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish("STOCK0");
- public void Run()
- {
- ...
- this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Subscribe(ShowNews,
- ThreadOption.UIThread);
- );
- }
- public void ShowNews(string companySymbol)
- {
- this.articlePresentationModel.SetTickerSymbol(companySymbol);
- }
public void Run()
{
...
this.eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Subscribe(ShowNews,
ThreadOption.UIThread);
);
}
public void ShowNews(string companySymbol)
{
this.articlePresentationModel.SetTickerSymbol(companySymbol);
}
subscribe还提供了用于过滤的参数,例如:
- FundAddedEvent fundAddedEvent = this.eventAggregator.GetEvent<FundAddedEvent>();
- fundAddedEvent.Subscribe(FundAddedEventHandler, ThreadOption.UIThread, false,
- fundOrder => fundOrder.CustomerId == this.customerId);