In my project I have three ViewModels (for example, ViewModelA
, ViewModelB
and ViewModelC
).
I need to write the following logic.
The ViewModelA
sends the value to ViewModelB
using the EventAggregator
from Prism.
The ViewModelB
receives the value and sends it to ViewModelC
.
The ViewModelC
receives the value and doing something.
Here is the code:
// 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?
}
}
PS: it is the part of the big project. So, please don’t suggest sending from ViewModelA
to ViewModelC
directly, without the ViewModelB
.