实现在C#中,可以使用EventAggregator(事件聚合器)来实现订阅发布模式。 ; public Publisher(IEventAggregator eventAggregator) { _eventAggregator = eventAggregator ; public Subscriber(IEventAggregator eventAggregator) { _eventAggregator = eventAggregator ; _eventAggregator.GetEvent<StateChangedEvent>().Subscribe(OnStateChanged); } private void = new EventAggregator(); // 创建发布者和订阅者 Publisher publisher = new Publisher(eventAggregator
} public class PersonInfoEven : PubSubEvent<PersonInfo> { } 03 订阅事件 IEventAggregator eventAggregator ;定义事件聚合器 然后获取事件聚合器实例 this.eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>(); 并通过IEventAggregator的GetEvent获取定义的消息,再通过Subscribe方法注册,Subscribe是一个委托方法 eventAggregator.GetEvent<PersonInfoEven (PubSubEventMessage); //发布事件方法 private void PubSubEventMessage() { eventAggregator.GetEvent Name ="zyr",Age = 18,Sex = "nan" }); } 这样,一个发布/订阅的事件就完成了,也可以在任何时候取消事件注册,只需要调用Unsubscribe方法即可 eventAggregator.GetEvent
在查看BaseBackgroundTask时,发现了令人惊喜的东西——IEventAggregator EventAggregator是何许玩意呢?按字面意思,事件聚合器?姑且这么叫吧! 为了弄清楚EventAggregator到底有什么用,我们先来看看与BaseEvent相关的几个类: 首先是一个事情订阅接口,包含一个订阅Token,一个获取可执行函数的方法。 { 7: Check.Argument.IsNotNull(eventAggregator, "eventAggregator"); 8: 9: _eventAggregator = eventAggregator; 10: } 11: 12: public bool IsRunning 关于发布,看看StoryService就可以了,在这个service的Create函数中有这么一段代码: 1: _eventAggregator.GetEvent<StorySubmitEvent
我还需要构建另外一个类型,我叫它 EventAggregator。 编写代码 创建 EventAggregator 在 coordinator 目录下添加 eventaggregator.go,代码如下: 第 28 行,建立 EventData struct,目前它的字段碰巧和 当事件发生的时候,EventAggregator 就轮流调用为该事件注册的回调函数; 第 9 行,就是 EventAggregator 的构造函数; 第 16 行,AddListener 方法 ,使用者通过该方法可以向 EventAggregator 注册回调函数; 第 20 行,PublishEvent 方法用来发布事件。 这里需要判断 EventAggregator 里是否已经注册了该事件,如果注册了,那么遍历其对应的回调函数,并使用事件数据进行调用。
;定义事件聚合器 然后通过ioc获取事件聚合器实例 this.eventAggregator = IoC.Get<IEventAggregator>(); 注意需要继承接口IHandle<PersonInfoEven > class StartViewModel : Caliburn.Micro.Screen, IShell,IHandle<PersonInfoEven> 然后订阅 this.eventAggregator.Subscribe ; public void EventTest() { this.eventAggregator = IoC.Get<IEventAggregator >(); //方法1 同步ui发布事件 //this.eventAggregator.PublishOnUIThread(new PersonInfoEven () { Name = "ZYR",Age=18,Sex ="man"}); //方法2 开线程去发布 this.eventAggregator.Publish
) { var testEvent = eventAggregator.GetEvent<TestEvent>(); testEvent.Subscribe(() ThreadOption.UIThread); } } public class TestEvent : PubSubEvent { } 上面是一段使用了 Prism 的单元测试,它主要的逻辑是在 EventAggregator 而 SynchronizationContext 又是在 EventAggregator 中赋值: private readonly SynchronizationContext syncContext 解决方案 现在我们知道问题原因了,解决方案也很简单,只要自定义一个 EventAggregator,源码全部照抄,但是把这句: private readonly SynchronizationContext , RegionNavigationContentLoader>(); ContainerExtension.RegisterSingleton<IEventAggregator, EventAggregator
() .Singleton<IWindowManager, WindowManager>() .Singleton<IEventAggregator, EventAggregator 5.1.1 视图模型 public class IndexVM : Screen, IHandle<BusyMessage> { private readonly IEventAggregator _eventAggregator ; public IndexVM(IEventAggregator eventAggregator) { _eventAggregator = eventAggregator Debug.WriteLine(@event); } 5.2.1 视图模型 public class LoginVM : Screen { private readonly IEventAggregator _eventAggregator ; public LoginVM(IEventAggregator eventAggregator) { _eventAggregator = eventAggregator
02重要知识点 CM框架的配置和启动; CM框架自带ioc容器SimpleContainer用法; MVVM开发模式; 事件聚合器EventAggregator进行事件发布和订阅; 属性通知: ① CM instance; } } 事件订阅:需要继承事件接口IHandle<PersonInfoEven> private readonly IEventAggregator eventAggregator ; this.eventAggregator = IoC.Get<IEventAggregator>(); this.eventAggregator.Subscribe(this); 事件处理方法 PersonInfo = message.ToString(); } ④SimpleContainerViewModel.cs 方法1 同步ui发布事件 this.eventAggregator.PublishOnUIThread ("i am a chinese"); 方法2 开线程去发布 this.eventAggregator.Publish(new PersonInfoEven() {
private IEventAggregator _eventAggregator; public ContactViewModel(IEventAggregator eventAggregator) { //获取框架内聚合事件的引用 _eventAggregator = eventAggregator; } //初始化订阅 _eventAggregator.GetEvent<MessagerEvent ) { _eventAggregator = eventAggregator; } //发布消息 _eventAggregator.GetEvent<MessagerEvent>().Publish { //获取框架内聚合事件的引用 _eventAggregator = eventAggregator; } //初始化订阅 _eventAggregator.GetEvent<MessagerEvent ) { _eventAggregator = eventAggregator; } //发布消息 _eventAggregator.GetEvent<MessagerEvent>().Publish
解耦是 MVVM 的一个重要目标,'EventAggregator' 则是实现解耦的重要工具。 要使用 EventAggregator,首先需要定义 PubSubEvent: public class TickerSymbolSelectedEvent : PubSubEvent<string>{ } 发布方和订阅方都通过 EventAggregator 索取 PubSubEvent,在 ViewModel中通常都是通过依赖注入获取一个 IEventAggregator: public class MainPageViewModel { IEventAggregator _eventAggregator; public MainPageViewModel(IEventAggregator ,其它的责任都不用管: _eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Publish("STOCK0"); 订阅方是真正使用这些消息并负责任的人
在Caliburn.Micro里EventAggregator要以单例的形式出现这样可以做到对广播做到统一的管理 对象实现IHand<T>接口后通过EventAggregator的subsribe方法把自己加入到 Handler集合中这样就能接叫信息 能过EventAggregator.Publish(object obj)方法去发送广播 源码: CaliburnIHandle.rar 先看一下个小demo再去分析它的源码是怎么实现的 </param> void Handle(TMessage message); } IHandle<T>只有一个处理T事件的的方法 EventAggregator类通过 //
目前 eventaggregator.go 里面包含了所有添加监听者以及向监听者发布事件的方法。 但现在的情况是事件的使用者也知道如何自行发布事件,这点不太好,因为它们不需要这样做。 相应的,后边所有涉及事件数据参数的地方都改为 interface{} 现在 EventAggregator 被泛化了,我也可以发布其它类型的事件了。 修改 queuelistener.go 里面的构造函数 让其传入 EventAggregator 作为参数并赋值给 QueueListener 的 ea 字段。 创建 EventAggregator,并传递给 DatabaseConsumer 和 QueueListener,让他们共享同一个 EventAggregator。
Resources.Apply(item => item.CurrentCulture = culture); IEventAggregator eventAggregator = IoC.Get<IEventAggregator>(); eventAggregator.Publish(new LanguageChangedMessage()); 方法里的 Resources.Apply(item => item.CurrentCulture = culture);是把所有实现IResult类的CurrentCulture修改成我们要换成的语言格式 eventAggregator.Publish
batch.AddExportedValue<IWindowManager>(new WindowManager()); batch.AddExportedValue<IEventAggregator>(new EventAggregator batch.AddExportedValue<IWindowManager>(new WindowManager()); batch.AddExportedValue<IEventAggregator>(new EventAggregator
Publishing and Subscribing using the EventAggregator is also described.
container.Singleton<IWindowManager, WindowManager>(); container.Singleton<IEventAggregator, EventAggregator 这里我们创建SimpleContainer并添加WindowManager和EventAggregator,当然还有ShellViewModel,但不是ShellView,因为我们有Assembly.Source.Instance
会停止闪烁 ③然后选中第一个shell页面: 这里在第一个输入框输入字符后click me就会使能,点击后会弹出子页面 在子页面点击publishevent可以发布事件消息: 事件发布点击后会在EventAggregator
Bootstrapper配置内容 Bootstrapper中有2个必需用的方法即: Initialize:初始化Bootstrapper所有设置,包括EventAggregator事件、AssemblySource IWindowManager>(new WindowManager());//新建一个窗口管理器添加到IOC中 batch.AddExportedValue<IEventAggregator>(new EventAggregator
batch.AddExportedValue<IWindowManager>(new WindowManager()); _batch.AddExportedValue<IEventAggregator>(new EventAggregator
ViewModel View 通知 ViewModel 推荐用数据绑定 尽量不要直接调用 ViewModel,但必要的时候也可以去调用 ViewModel 通知 View 属性绑定 事件通知 消息(比如 EventAggregator