Event Aggregator in Prism (Composite Application Guidance for WPF)
2013-11-22 15:46
561 查看
The EventAggregator service is primarily a container for events that allow decoupling of publishers and subscribers so they can evolve independently. This decoupling is useful in modularized applications because new modules can be added that respond to events defined by the shell or, more likely, other modules.
In the Composite Application Library, EventAggregator allows subscribers or publishers to locate a specific EventBase. The event aggregator also allows for multiple publishers and multiple subscribers, as shown below.
The EventAggregator class is offered as a service in the container and can be retrieved through the IEventAggregator interface. The event aggregator is responsible for locating or building events and for keeping a collection of the events in the system.
1) Let's get started with example below, at first we have to add refreences to namespaces below,
2) Event declaration and Initialization
3) Event Subscription
4) Event Publication
5) Event unsubscription
To get more details, you can refer to : http://www.codeplex.com/wikipage?ProjectName=CompositeWPF&title=Getting%20Started&referringTitle=Home
In the Composite Application Library, EventAggregator allows subscribers or publishers to locate a specific EventBase. The event aggregator also allows for multiple publishers and multiple subscribers, as shown below.
The EventAggregator class is offered as a service in the container and can be retrieved through the IEventAggregator interface. The event aggregator is responsible for locating or building events and for keeping a collection of the events in the system.
public interface IEventAggregator { TEventType GetEvent<TEventType>() where TEventType : EventBase; }
1) Let's get started with example below, at first we have to add refreences to namespaces below,
using Microsoft.Practices.Composite.Events; using Microsoft.Practices.Composite.Presentation.Events;
2) Event declaration and Initialization
public class MyStringEvent : CompositePresentationEvent<string> { } public class MyIntEvent : CompositePresentationEvent<int> { }
IEventAggregator eventAggregator = new EventAggregator(); SubscriptionToken stringToken = new SubscriptionToken(); SubscriptionToken integerToken = new SubscriptionToken();
3) Event Subscription
stringToken = eventAggregator.GetEvent<MyStringEvent>().Subscribe(s => MessageBox.Show("this is " + s)); integerToken = eventAggregator.GetEvent<MyIntEvent>().Subscribe(i => MessageBox.Show("this is " + i.ToString()));
4) Event Publication
eventAggregator.GetEvent<MyStringEvent>().Publish("MyString"); eventAggregator.GetEvent<MyIntEvent>().Publish(1000);
5) Event unsubscription
eventAggregator.GetEvent<MyStringEvent>().Unsubscribe(stringToken); eventAggregator.GetEvent<MyIntEvent>().Unsubscribe(integerToken);
To get more details, you can refer to : http://www.codeplex.com/wikipage?ProjectName=CompositeWPF&title=Getting%20Started&referringTitle=Home
相关文章推荐
- [Prism]Composite Application Guidance for WPF(5)——依赖注入容器
- [Prism]Composite Application Guidance for WPF(5)——依赖注入容器
- [Prism]Composite Application Guidance for WPF(7)——模块
- [Prism]Composite Application Guidance for WPF(9)——命令
- [转][Prism]Composite Application Guidance for WPF(6)——服务
- The Composite Application Guidance for WPF(prism)
- [Prism]Composite Application Guidance for WPF(7)——模块
- [Prism]Composite Application Guidance for WPF(9)——命令
- [Prism]Composite Application Guidance for WPF(4)——Bootstrapper
- [Prism]Composite Application Guidance for WPF(4)——Bootstrapper
- [Prism]Composite Application Guidance for WPF(6)——服务
- [Prism]Composite Application Guidance for WPF(2)--Composite Application Library(CAL)
- [Prism]Composite Application Guidance for WPF(10)——系列目录导航
- [Prism]Composite Application Guidance for WPF(2)--Composite Application Library(CAL)
- [Prism]Composite Application Guidance for WPF(6)——服务
- [Prism]Composite Application Guidance for WPF(8)——事件
- [Prism]Composite Application Guidance for WPF(10)——系列目录导航
- [Prism]Composite Application Guidance for WPF(8)——事件
- [Prism]Composite Application Guidance for WPF(1)--概览
- [Prism]Composite Application Guidance for WPF(3)——创建第一个Composite WPF Application