0
votes

I have problem to understand how to implement this with Caliburn Micro, event aggregator, i have a view model that call 2 different window, and before show, I subscribe to an handle of type string, it works, but now I want to know in the handle method, from which subscribe came the message:

    public void Causale()
    {
        var asm = Assembly.LoadFrom(@"V.M.Tabelle.Magazzino.Causale.dll");
        var module = _shell.LoadModule(asm);

        if (module != null)
        {
            _eventAggregator.Subscribe(this);

            module.Show("Add");

            //_shell.CurrentView = (new ShellMenuItem { Caption = "Tiard", ScreenViewModel = module });
        }
    }

    public void CausaleList()
    {
        var asm = Assembly.LoadFrom(@"V.M.Tabelle.Magazzino.Causale.dll");
        var module = _shell.LoadModule(asm);

        if (module != null)
        {
            _eventAggregator.Subscribe(this);

            module.Show("List");

            //_shell.CurrentView = (new ShellMenuItem { Caption = "Tiard", ScreenViewModel = module });
        }
    }


    public void Handle(string message)
    {
        _eventAggregator.Unsubscribe(this);

        BackValue = message;
        NotifyOfPropertyChange(() => BackValue);
    }

I've serached for a property, like subscribe(this).name("") to pass something like a token.

Thank you

2

2 Answers

1
votes

Instead of subscribing to a type of string, you could create a custom class which includes the message and the sender information, and then subscribe to it. For example,

public class ActionMessage
{
    public object Sender{get;set;}
    public string Message{get;set;}
}

You could now publish the Message as,

_eventAggregator.PublishOnUIThread(new ActionInvokedMessage { Message = "Add", Sender = this });

This Sender property would include the source of message. You could verify the type of source when handling the event.

public void Handle(ActionInvokedMessage message)
{
        if(message.Sender is UserControl1ViewModel)
        {

        }

        if(message.Sender is UserControl2ViewModel)
        {

        }
}
0
votes

Eventaggregator passes the message as a parameter to the subscriber.

Hence your method:

public void Handle(string message)

Will receive either "Add" or "List" as the message parameter.

That parameter can be any type you like. That type can be a complex type rather than a value type. In fact it is usually a good idea to define a class ( type ) for each sort of message you want to send since it is the type which differentiates which handlers are fired when any message is raised.

In any case, if string is not enough information somehow you could instead define a class with numerous properties and pass as many parameters you like in that way. EG you could have class Foo with Mode and Source properties. Mode being Add/List and Source being wherever the message originates from.