Search code examples
c#caliburn.microeventaggregator

Caliburn.Micro EventAggregator with multiple instances


I have two a Caliburn.Micro ViewModels communicating each other by EventAggregator:

public class LeftViewModel
{
    // ...
    public void DoInRight()
    {
        messanger.PublishOnUIThread(new DoInRightMessage());
    }
}

public class RightViewModel : IHandle<DoInRightMessage>
{
    public void Handle(DoInRightMessage e)
    {
        // ...
    }
}

It has worked fine until I had just one instance of LeftViewModel and RighViewModel on the screen. But now I would like to allow arbitrary number of instances of both of them - always in couples. So LeftViewModel.DoInRight() should send the message only in one RighViewModel (the one in the couple), not all RighViewModel instances.

Update 8.7.2015: How to solve such pair messaging:

  • Some kind of message channels/groups by adding a parameter target or id to the messages (thanks @jophy job for point me to the idea).
  • Have multiple EventAggregators.
  • Abandon the EventAggregator and call methods directly.
  • Or any other solution?

Solution

  • You can try using some sort of ID for every View.

    there is a similar question Caliburn.Micro and event aggregator -unwanted call handle method