我有四个 View 模型 M1、M2、M3、M4,我从那里向另一个 View 模型 M5 发送消息。 M5 在收到来自其中任何一个的消息时执行相同的操作。目前,我在 M5 中编写了这样的代码:
MessgingCenter.Subscribe<M1, string>(this, "abc", () => { DoSomething(); });
MessagingCenter.Subscribe<M2, string>(this, "abc", () => { DoSomething(); });
MessgingCenter.Subscribe<M3, string>(this, "abc", () => { DoSomething(); });
MessagingCenter.Subscribe<M4, string>(this, "abc", () => { DoSomething(); });
我可以单行实现吗?
最佳答案
可以从同一个父ViewModel(或接口(interface))继承M1、M2、M3、M4,名称如“BaseViewModel”,然后写:
MessagingCenter.Subscribe<BaseViewModel, string>(this, "abc", () => { DoSomething(); });
关于c# - Xamarin : Subscribing to multiple messages with same action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44812892/