首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏后端

    C# 一分钟浅谈:观察者模式与订阅发布模式

    实现在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

    94610编辑于 2024-10-23
  • 来自专栏dotNET编程大全

    C# WPF MVVM模式Prism框架下事件发布与订阅

    } 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

    4.9K20编辑于 2022-01-13
  • 来自专栏JadePeng的技术博客

    ASP.NET MVC 开源项目Kigg解读(1)

    在查看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

    1.2K60发布于 2018-03-12
  • 来自专栏草根专栏

    RabbitMQ 入门 (Go) - 5. 使用 Fanout Exchange 做服务发现(下)

    我还需要构建另外一个类型,我叫它 EventAggregator。 编写代码 创建 EventAggregator 在 coordinator 目录下添加 eventaggregator.go,代码如下: 第 28 行,建立 EventData struct,目前它的字段碰巧和 当事件发生的时候,EventAggregator 就轮流调用为该事件注册的回调函数; 第 9 行,就是 EventAggregator 的构造函数; 第 16 行,AddListener 方法 ,使用者通过该方法可以向 EventAggregator 注册回调函数; 第 20 行,PublishEvent 方法用来发布事件。 这里需要判断 EventAggregator 里是否已经注册了该事件,如果注册了,那么遍历其对应的回调函数,并使用事件数据进行调用。

    55230发布于 2021-11-02
  • 来自专栏dotNET编程大全

    C# WPF MVVM模式Caliburn.Micro框架下事件发布与订阅

    ;定义事件聚合器 然后通过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

    2.9K10编辑于 2022-01-13
  • 来自专栏dino.c的专栏

    在单元测试中使用 Prism 的 EventAggregator,订阅到 ThreadOption.UIThread 会报错

    ) { 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

    1.1K30发布于 2021-01-27
  • 来自专栏历史专栏

    【愚公系列】2023年02月 .NET CORE工具案例-Caliburn.Micro的使用基于WPF的改造的MVVM案例

    () .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

    2K20编辑于 2023-03-16
  • 来自专栏dotNET编程大全

    C# 值得永久收藏的WPF项目实战(经典)

    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() {

    6.1K10编辑于 2021-12-17
  • 来自专栏JusterZhu

    08Prism WPF 入门实战 - Cmd&EeventAggregator

    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

    55210编辑于 2022-12-07
  • 来自专栏dino.c的专栏

    [Windows] Prism 8.0 入门(上):Prism.Core

    解耦是 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"); 订阅方是真正使用这些消息并负责任的人

    2.8K60发布于 2020-12-08
  • 来自专栏技术之路

    Caliburn.Micro学习笔记(三)----事件聚合IEventAggregator和 Ihandle<T>

    在Caliburn.Micro里EventAggregator要以单例的形式出现这样可以做到对广播做到统一的管理 对象实现IHand<T>接口后通过EventAggregator的subsribe方法把自己加入到 Handler集合中这样就能接叫信息 能过EventAggregator.Publish(object obj)方法去发送广播 源码: CaliburnIHandle.rar 先看一下个小demo再去分析它的源码是怎么实现的 </param> void Handle(TMessage message); } IHandle<T>只有一个处理T事件的的方法 EventAggregator类通过 //

    1.8K90发布于 2018-01-31
  • 来自专栏草根专栏

    RabbitMQ 入门 (Go) - 6. 数据持久化(上)

    目前 eventaggregator.go 里面包含了所有添加监听者以及向监听者发布事件的方法。 但现在的情况是事件的使用者也知道如何自行发布事件,这点不太好,因为它们不需要这样做。 相应的,后边所有涉及事件数据参数的地方都改为 interface{} 现在 EventAggregator 被泛化了,我也可以发布其它类型的事件了。 修改 queuelistener.go 里面的构造函数 让其传入 EventAggregator 作为参数并赋值给 QueueListener 的 ea 字段。 创建 EventAggregator,并传递给 DatabaseConsumer 和 QueueListener,让他们共享同一个 EventAggregator

    77260发布于 2021-11-02
  • 来自专栏技术之路

    Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能

    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

    1.1K70发布于 2018-01-31
  • 来自专栏dotNET编程大全

    C# WPF Caliburn.Micro框架下利用Mef加载其它项目界面

    batch.AddExportedValue<IWindowManager>(new WindowManager()); batch.AddExportedValue<IEventAggregator>(new EventAggregator batch.AddExportedValue<IWindowManager>(new WindowManager()); batch.AddExportedValue<IEventAggregator>(new EventAggregator

    1.7K40发布于 2021-11-01
  • 来自专栏张善友的专栏

    Prism Training Kit 4.0

    Publishing and Subscribing using the EventAggregator is also described.

    839100发布于 2018-01-31
  • 来自专栏dotNET编程大全

    C# WPF MVVM开发框架Caliburn.Micro自定义引导程序④

    container.Singleton<IWindowManager, WindowManager>(); container.Singleton<IEventAggregator, EventAggregator 这里我们创建SimpleContainer并添加WindowManager和EventAggregator,当然还有ShellViewModel,但不是ShellView,因为我们有Assembly.Source.Instance

    1.4K10编辑于 2022-01-13
  • 来自专栏dotNET编程大全

    C# WPF CM框架从入门到精通(经典)

    会停止闪烁 ③然后选中第一个shell页面: 这里在第一个输入框输入字符后click me就会使能,点击后会弹出子页面 在子页面点击publishevent可以发布事件消息: 事件发布点击后会在EventAggregator

    1.9K40编辑于 2022-04-19
  • 来自专栏全栈程序员必看

    Caliburn.Micro Bootstrapper及IOC容器配置

    Bootstrapper配置内容 Bootstrapper中有2个必需用的方法即: Initialize:初始化Bootstrapper所有设置,包括EventAggregator事件、AssemblySource IWindowManager>(new WindowManager());//新建一个窗口管理器添加到IOC中 batch.AddExportedValue<IEventAggregator>(new EventAggregator

    1.3K30编辑于 2022-09-09
  • 来自专栏技术之路

    Caliburn.Micro学习笔记(一)----引导类和命名匹配规则

    batch.AddExportedValue<IWindowManager>(new WindowManager()); _batch.AddExportedValue<IEventAggregator>(new EventAggregator

    1.6K80发布于 2018-01-31
  • 来自专栏walterlv - 吕毅的博客

    当我们使用 MVVM 模式时,我们究竟在每一层里做些什么?

    ViewModel View 通知 ViewModel 推荐用数据绑定 尽量不要直接调用 ViewModel,但必要的时候也可以去调用 ViewModel 通知 View 属性绑定 事件通知 消息(比如 EventAggregator

    1.2K10发布于 2018-09-18
领券