• EventBroker


    Sample publisher

    Publish an event topic:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class Publisher
    {
        [EventPublication("topic://EventBrokerSample/SimpleEvent")]
        public event EventHandler SimpleEvent;
         
        public void FireSimpleEvent()
        {
            SimpleEvent(this, EventArgs.Empty);
        }
    }

    Register the publisher with your event broker (you have to hold an instance of the event broker somewhere in your code).

    1
    2
    3
    EventBroker eventBroker = ...;
    Publisher publisher = new Publisher();
    eventBroker.Register(publisher);

    On registration of the publisher, the event broker inspects the publisher for published events (events with the EventPublication attribute).

    Sample subscriber

    Subscribe to an event topic:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Subscriber
    {
        [EventSubscription(
            typeof(OnPublisher))]
     
        public void SimpleEvent(object sender, EventArgs e)
        {
            // do something useful or at least funny
        }
    }

    Register the subscriber with the event broker:

    1
    2
    3
    EventBroker eventBroker = ...;
    Subscriber subscriber = new Subscriber();
    eventBRoker.Register(subscriber);

    The event broker will inspect the subscriber on registration for subscription to event topics (methods with the EventSubscription attribute).

    If a publisher fires an event topic for that subscribers are registered, then the event broker will relay them to the subscribers by calling the subscription handler methods with the sender and EventArgs the publisher used to fire its event.

    Publication options

    Simple

    1
    2
    [EventPublication("topic://Simple")]
    public event EventHandler SimpleEvent;

    With custom Eventargs

    Note: CustomEventArgs has simply to be derived from EventArgs.

    1
    2
    [EventPublication("topic://CustomEventArgs")]
    public event EventHandler<CustomEventArguments> CustomEventArgs;

    Publish multiple event topics with one single event

    1
    2
    3
    4
    [EventPublication("Event1")]
    [EventPublication("Event2")]
    [EventPublication("Event3")]
    public event EventHandler MultiplePublicationTopics;

    Allow only synchronous subscription handlers

    1
    2
    [EventPublication("topic://Simple", HandlerRestriction.Synchronous)]
    public event EventHandler AnEvent;

    Allow only asynchronous subscription handlers

    1
    2
    [EventPublication("topic://Simple", HandlerRestriction.Asynchronous)]
    public event EventHandler AnEvent;

    Subscription options

    Simple

    1
    2
    [EventSubscription("topic://Simple", typeof(OnPublisher)]
    public void SimpleEvent(object sender, EventArgs e) {}

    Custom EventArgs

    1
    2
    [EventSubscription("topic://CustomEventArgs"), typeof(OnPublisher))]
    public void CustomEventArgs(object sender, CustomEventArgs e) {}

    Subscribe multiple event topics

    1
    2
    3
    4
    [EventSubscription("Event1", typeof(OnPublisher))]
    [EventSubscription("Event2", typeof(OnPublisher))]
    [EventSubscription("Event3", typeof(OnPublisher))]
    public void MultipleSubscriptionTopics(object sender, EventArgs e) {}

    Execute handler on background thread (asynchronous)

    The event broker creates a worker thread to execute the handler method on. The publisher can immediately continue processing.

    1
    2
    [EventSubscription("topic://Background", typeof(OnBackground))]
    public void BackgroundThread(object sender, EventArgs e) {}

    Execute handler on UI thread

    Use this option if calling from a background worker thread to a user interface component that updates the user interface - no need for Control.Invoke(...) anymore.

    1
    2
    [EventSubscription("topic://UI", typeof(OnUserInterface))]
    public void UI(object sender, EventArgs e) {}

    Note that if you use the OnUserInterface handler, you have to make sure that you register the subscriber on the user interface thread. Otherwise, the EventBroker won't be able to switch to the user interface thread, and will throw an exception.

    Execute handler on UI thread asynchronously

    The same as above, but the publisher is not blocked until the subscriber has processed the event.

    1
    2
    [EventSubscription("topic://UIAsync", typeof(OnUserInterfaceAsync))]
    public void UI(object sender, EventArgs e) {} 

    Simplified subscription handler signatures

    If you are not interested in the sender of the event, you can leave the sender out in the handler method:

    1
    2
    [EventSubscription("topic://handle.me", typeof(OnPublisher))]
    public void Handle(EventArgs e) {} 

    If you also don't need the event arguments, you can ignore them, too:

    1
    2
    [EventSubscription("topic://handle.me", typeof(OnPublisher))]
    public void Handle() {} 

    And if you have a generic event argument EventArgs<T>, you can directly define the content value of the event arguments:

    1
    2
    3
    4
    5
    [EventPublication("topic://handle.me")]
    public event EventHandler<EventArgs<string>> Event;
     
    [EventSubscription("topic://handle.me", typeof(OnPublisher))]
    public void Handle(string value) {} 

    These are the basics about the EventBroker. See the rest of the documentation for more options and details.

  • 相关阅读:
    android 面试题
    android 入门-android Studio git 克隆
    android 入门-android自定义控件
    android 入门-控件 测量状态栏高度
    android 入门-android Studio git配置
    WPF 介绍一种在MVVM模式下弹出子窗体的方式
    WPF DataGrid分组和排序
    C# 等待框
    WPF 简洁的主界面
    C# 在WPF中使用Exceptionless异常日志框架
  • 原文地址:https://www.cnblogs.com/zeroone/p/4979313.html
Copyright © 2020-2023  润新知