Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 棱镜事件聚合器。接收事件并再次发送_C#_Wpf_Mvvm_Prism_Eventaggregator - Fatal编程技术网

C# 棱镜事件聚合器。接收事件并再次发送

C# 棱镜事件聚合器。接收事件并再次发送,c#,wpf,mvvm,prism,eventaggregator,C#,Wpf,Mvvm,Prism,Eventaggregator,在我的项目中,我有三个视图模型(例如,ViewModelA,ViewModelB和ViewModelC)。 我需要编写以下逻辑。 ViewModelA使用Prism的EventAggregator将值发送到ViewModelB。 ViewModelB接收该值并将其发送到ViewModelC ViewModelC接收值并执行操作 代码如下: // The data that will be send using the event aggregator. class EventData : Pub

在我的项目中,我有三个视图模型(例如,
ViewModelA
ViewModelB
ViewModelC
)。
我需要编写以下逻辑。
ViewModelA
使用Prism的
EventAggregator
将值发送到
ViewModelB

ViewModelB
接收该值并将其发送到
ViewModelC

ViewModelC
接收值并执行操作

代码如下:

// The data that will be send using the event aggregator.
class EventData : PubSubEvent<int>
{ 
}

class ViewModelA
{
    IEventAggregator m_eventAggregator;

    public ViewModelA(IEventAggregator eventAggregator)
    {
        m_eventAggregator = eventAggregator;

        // Publish some value.
        eventAggregator.GetEvent<EventData>().Publish(10);
    }
}

class ViewModelB
{
    IEventAggregator m_eventAggregator;

    public ViewModelB(IEventAggregator eventAggregator)
    {
        m_eventAggregator = eventAggregator;

        eventAggregator.GetEvent<EventData>().Subscribe(OnDataReceived);
    }

    void OnDataReceived(int value)
    {
        // Here I want to send the value to the ViewModelC. How can I do it?
    }
}
//将使用事件聚合器发送的数据。
类EventData:PubSubEvent
{ 
}
类ViewModelA
{
事件聚合器;
公共视图模型A(IEventAggregator事件聚合器)
{
m_eventAggregator=eventAggregator;
//发布一些价值。
eventAggregator.GetEvent().Publish(10);
}
}
类ViewModelB
{
事件聚合器;
公共视图模型B(IEventAggregator事件聚合器)
{
m_eventAggregator=eventAggregator;
eventAggregator.GetEvent().Subscribe(OnDataReceived);
}
无效OnDataReceived(int值)
{
//在这里,我想将值发送到ViewModelC。我如何才能做到这一点?
}
}

附言:这是大项目的一部分。因此,请不要建议直接从
ViewModelA
发送到
ViewModelC
,如果没有
ViewModelB
,事件聚合器的工作方式是每个人都可以收听每个事件。因此,如果要从A发送到B(然后从B发送到C),需要两个不同的“私有”事件

A:

eventAggregator.Publish(值);
B:

eventAggregator.Subscribe(x=>{
var y=过程(x);
eventAggregator.Publish(y);
} );
C:

eventAggregator.Subscribe(x=>whatever(x));
旁注:您发布和订阅的是事件类型,而不是负载类型。事件(
EventData
)的命名有点笨拙,因为会将其与负载混淆。更好的名称应该是
DataEvent
。。。或者,在这里,
RawDataEvent
ProcessedDataEvent

旁注2:显然,事件聚合器不适用于非广播消息。您可以强制它(使用不同的事件类型,或者,例如,通过在负载中包含目标id),但我更喜欢为点对点消息设计的消息传递系统

 eventAggregator.Publish<AtoB>( value );
 eventAggregator.Subscribe<AtoB>( x => {
                                           var y = process(x);
                                           eventAggregator.Publish<BtoC>( y );
                                       } );
 eventAggregator.Subscribe<BtoC>( x => whatever( x ) );