使用 Messenger 将 IOrder 从 ViewModel1 发送到 ViewModel2,如何区分 ADD/DEL?
我有一个 CustomerListViewModel 和一个 OrderListViewModel。在后者中,我选择一个订单将其删除或创建一个新订单。在这两种情况下,我的 CustomerListViewModel 和 Messenger 必须注册到 IOrder 类型:
Messenger.Default.Register<IOrder>(this, AddOrder);
Messenger.Default.Register<IOrder>(this, DeleteOrder);
In the OrderListViewModel I do send the customer to be deleted/added:
Messenger.Default.Send<IOrder>(MyOrderObject);
Now both Actions AddOrder and DeleteOrder gets surely executed, how can I differentiate ?
I have a CustomerListViewModel and a OrderListViewModel. In the latter I select an order to delete it or I create a new one. In both situations my CustomerListViewModel and the Messenger must register to the type IOrder:
Messenger.Default.Register<IOrder>(this, AddOrder);
Messenger.Default.Register<IOrder>(this, DeleteOrder);
In the OrderListViewModel I do send the customer to be deleted/added:
Messenger.Default.Send<IOrder>(MyOrderObject);
Now both Actions AddOrder and DeleteOrder gets surely executed, how can I differentiate ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是插入或删除,所有者不会发送任何提示。因此,接收者只能自己猜测或发现。
我的建议是引入额外的消息类型(IAddOrder、IDeleteOrder),让发送者有一个接口来告知更改的类型。
如果不可能,您需要将一些信息添加到 IOrder 中,但这只是一个令人讨厌的解决方法,因为交换消息后不再需要数据。
The owner does not send any hint, if it is insert or delete. Hence, the receiver can only guess or find out by himself.
My suggestion is to introduce additional message-types (IAddOrder, IDeleteOrder), s.t. the sender has an interface to tell about the type of change.
If this is not possibe, you would need to add some information into the IOrder, but that would be just a smelly workaround, because the data is no longer needed after exchanging the message.