gpt4 book ai didi

c# - 在将对象作为有效负载的 View 模型之间使用Prism 6 Event Aggregator

转载 作者:行者123 更新时间:2023-12-03 10:26:27 25 4
gpt4 key购买 nike

嗨,我希望你们中的一个可以帮助我一些语法。

所以我在使用Prism Event Aggregator将消息从一个ViewModel发送到另一个ViewModel时遵循了这个示例https://www.codeproject.com/Tips/591221/Simple-EventAggregator-in-WPF-PRISM

当发布单个字符串作为有效负载时,以下代码非常有用。

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; }
}
}

发布者(在ViewModel1中):
OrderSelectedEvent_SS.Instance.Publish(_strCurrentOrder);

订阅者(在ViewModel2中:
OrderSelectedEvent_SS.Instance.Subscribe(OrderSelectedEventHandler_SS);
private void OrderSelectedEventHandler_SS(string strSelectedOrder)
{
CurrentOrder = strSelectedOrder;
}

我正在尝试做的是修改以接受对象(具有多个字符串的类)作为有效负载。
这是我到目前为止的内容:
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; }
}
}

发布者(在ViewModel1中):
OrderSelectedEvent.OrderSelectedPayload ospl = new OrderSelectedEvent.OrderSelectedPayload();
ospl.SelectedOrder = _strCurrentOrder;
ospl.SelectedE32 = _strCurrentE32JobNumber;
OrderSelectedEvent.Instance.Publish(ospl);

订阅者(在ViewModel2中:
OrderSelectedEvent.Instance.Subscribe(OrderSelectedEventHandler); 

我在subsriber上遇到错误:**错误:无法从“方法组”转换为“操作” ****,而且我似乎无法弄清楚我需要做什么。
private void OrderSelectedEventHandler(OrderSelectedEvent.OrderSelectedPayload ospl)
{
CurrentOrder = ospl.SelectedOrder;
CurrentE32JobNumber = ospl.SelectedE32;
}

在此先感谢您提供的任何指导。

最佳答案

您的问题很简单:您正在定义事件以将object传递为有效负载,然后尝试使用特定的类作为有效负载。要解决此问题,请更改

internal class OrderSelectedEvent : PubSubEvent<object>

到:
internal class OrderSelectedEvent : PubSubEvent<OrderSelectedPayload>

另外,您可以更改处理程序签名以匹配事件的预期参数:
private void OrderSelectedEventHandler(OrderSelectedEvent.OrderSelectedPayload ospl)

到:
private void OrderSelectedEventHandler(object ospl)

关于c# - 在将对象作为有效负载的 View 模型之间使用Prism 6 Event Aggregator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com