开发者

Send an IOrder from ViewModel1 to ViewModel2 with a Messenger, how do you differentiate ADD/DEL?

开发者 https://www.devze.com 2023-01-20 02:11 出处:网络
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

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 se开发者_开发技巧nd the customer to be deleted/added:

Messenger.Default.Send<IOrder>(MyOrderObject);

Now both Actions AddOrder and DeleteOrder gets surely executed, how can I differentiate ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消