c# - Xamarin : Subscribing to multiple messages with same action

标签 c# xamarin xamarin.forms

我有四个 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/

相关文章:

c# - AES 算法中的加密文本太大

c# - XamlReader.Load(..) 时出现异常

c# - LINQ 查询 SQLite 时出错

ios - ITMS-90809:不推荐使用的API使用情况(UIWebView)

c# - 错误 CS0433 : The type 'JsonValue' exists in both 'System.Json, Version=2.0.5.0' and 'Xamarin.Auth' (CS0433)

c# - 如何将复杂对象从 jquery 传递到标记 <a> 中的 Action Controller

c# - 防伪 token 问题已解决但对项目产生影响后很奇怪

macos - Xamarin Monodevelop - 错误 CS0103

Xamarin.forms webview 自动高度与内容

c# - Xamarin 表单和 Android 蓝牙