本文介绍了在 Prism EventAggregator 中发布没有 PayLoad 的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不能在没有任何 PayLoad 的情况下发布事件.

Why can't we Publish Events without any PayLoad.

    _eventAggregator.GetEvent<SelectFolderEvent>().Publish(new SelectFolderEventCriteria() { });

现在,我不需要在这里传递任何有效载荷.但是 EventAggregator 实现要求我有一个空类来做到这一点.

Now, I don't need any pay load to be passed here. But the EventAggregator implementation mandates me to have an empty class to do that.

事件:

  public class SelectFolderEvent : CompositePresentationEvent<SelectFolderEventCriteria>
  {
  }

有效载荷:

  public class SelectFolderEventCriteria
  {
  }

为什么 Prism 没有给出只使用 Event 并像这样发布它的方法

Why has Prism not given a way to use just the Event and publish it like

    _eventAggregator.GetEvent<SelectFolderEvent>().Publish();

是有意为之而我不明白吗?请解释.谢谢!

Is it by design and I don't understand it?Please explain. Thanks!

推荐答案

好问题,我没有看到不发布没有负载的事件的理由.在某些情况下,事件已引发这一事实就是您需要和想要处理的所有信息.

Good question, I don't see a reason for not publishing an event without a payload. There are cases where the fact that an event has been raised is all information you need and want to handle.

有两种选择:因为它是开源的,所以您可以使用 Prism 源并提取一个不需要负载的 CompositePresentation 事件.

There are two options: As it is open source, you can take the Prism source and extract a CompositePresentation event that doesn't take a payload.

我不会那样做,而是将 Prism 作为 3rd 方库处理并保持原样.为第 3 方库编写 Facade 以使其适合您的项目是一种很好的做法,在这是 CompositePresentationEvent 的例子.这可能看起来像这样:

I wouldn't do that, but handle Prism as a 3rd party library and leave it as it is. It is good practice to write a Facade for a 3rd party library to fit it into your project, in this case for CompositePresentationEvent. This could look something like this:

public class EmptyPresentationEvent : EventBase
{
    /// <summary>
    /// Event which facade is for
    /// </summary>
    private readonly CompositePresentationEvent<object> _innerEvent;

    /// <summary>
    /// Dictionary which maps parameterless actions to wrapped
    /// actions which take the ignored parameter
    /// </summary>
    private readonly Dictionary<Action, Action<object>> _subscriberActions;

    public EmptyPresentationEvent()
    {
        _innerEvent = new CompositePresentationEvent<object>();
        _subscriberActions = new Dictionary<Action, Action<object>>();
    }

    public void Publish()
    {
        _innerEvent.Publish(null);
    }

    public void Subscribe(Action action)
    {
        Action<object> wrappedAction = o => action();
        _subscriberActions.Add(action, wrappedAction);
        _innerEvent.Subscribe(wrappedAction);
    }

    public void Unsubscribe(Action action)
    {
        if (!_subscriberActions.ContainsKey(action)) return;
        var wrappedActionToUnsubscribe = _subscriberActions[action];
        _innerEvent.Unsubscribe(wrappedActionToUnsubscribe);
        _subscriberActions.Remove(action);
    }
}

有什么不清楚的,请追问.

If anything is unclear, please ask.

这篇关于在 Prism EventAggregator 中发布没有 PayLoad 的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:37