Using Prism 6 Event Aggregator between view models with an object as the payload

孤人 提交于 2019-12-23 03:46:10

问题


Hi I'm hoping one of you could help me with some syntax.

So I'm following this example https://www.codeproject.com/Tips/591221/Simple-EventAggregator-in-WPF-PRISM on using Prism Event Aggregator to send messages from one ViewModel to another.

The following code works great when publishing a single string as the payload.

internal class OrderSelectedEvent_SS : PubSubEvent<string>
{
    private static readonly EventAggregator _eventAggregator;
    private static readonly OrderSelectedEvent_SS _event;

    static OrderSelectedEvent_SS()
    {
        _eventAggregator = new EventAggregator();
        _event = _eventAggregator.GetEvent<OrderSelectedEvent_SS>();
    }

    public static OrderSelectedEvent_SS Instance
    {
        get { return _event; }
    }
}

Publisher (In ViewModel1):

OrderSelectedEvent_SS.Instance.Publish(_strCurrentOrder);

Subscriber (In ViewModel2:

OrderSelectedEvent_SS.Instance.Subscribe(OrderSelectedEventHandler_SS);
private void OrderSelectedEventHandler_SS(string strSelectedOrder)
    {
        CurrentOrder = strSelectedOrder;
    }

What I'm trying to do is modify to accept an object (a class with multiple strings) as the payload. Here's what I have so far:

internal class OrderSelectedEvent : PubSubEvent<object>
{
    private static readonly EventAggregator _eventAggregator;
    private static readonly OrderSelectedEvent _event;
    static OrderSelectedEvent()
    {
        _eventAggregator = new EventAggregator();
        _event = _eventAggregator.GetEvent<OrderSelectedEvent>();
    }

    public static OrderSelectedEvent Instance
    {
        get { return _event; }
    }

    public class OrderSelectedPayload
    {
        public string SelectedOrder { get; set; }
        public string SelectedE32 { get; set; }
    }
}

Publisher (In ViewModel1):

OrderSelectedEvent.OrderSelectedPayload ospl = new OrderSelectedEvent.OrderSelectedPayload();
ospl.SelectedOrder = _strCurrentOrder;
ospl.SelectedE32 = _strCurrentE32JobNumber;
OrderSelectedEvent.Instance.Publish(ospl);

Subscriber (In ViewModel2:

OrderSelectedEvent.Instance.Subscribe(OrderSelectedEventHandler); 

I'm getting an error here on the subsriber: **ERROR: cannot convert from 'method group' to 'Action'**** and I can't seem to figure out what I need to do.

private void OrderSelectedEventHandler(OrderSelectedEvent.OrderSelectedPayload ospl)
{
    CurrentOrder = ospl.SelectedOrder;
    CurrentE32JobNumber = ospl.SelectedE32;
}

Thanks in advance for any guidance you can provide.


回答1:


Your problem is simple: You are defining the event to pass an object as payload, then trying to use a specific class as payload instead. To fix this, change

internal class OrderSelectedEvent : PubSubEvent<object>

To:

internal class OrderSelectedEvent : PubSubEvent<OrderSelectedPayload>

Alternatively, you could change your handler signature to match the expected parameters of the event:

private void OrderSelectedEventHandler(OrderSelectedEvent.OrderSelectedPayload ospl)

To:

private void OrderSelectedEventHandler(object ospl)


来源:https://stackoverflow.com/questions/42451146/using-prism-6-event-aggregator-between-view-models-with-an-object-as-the-payload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!