如何以Xamarin形式使用消息传递中心 [英] How to use Messaging center in xamarin forms
问题描述
我正在尝试使用消息中心而不是xamarin形式的Messenger,我对消息中心一无所知,我曾尝试使用Bellow代码以xamarin形式进行订阅和发送消息
I am trying to use messaging center instead of Messenger in xamarin forms I have no idea about messaging center I tried Bellow code to subscribe and Send Message in xamarin forms
MessagingCenter.Send(this, "TodoTable", "Todo");
但是我不知道从哪里可以订阅以下消息,我尝试了下面的代码:
But I have not Idea from where I can subscribe to this message I tried bellow code :
MessagingCenter.Subscribe<TodoClass>(this, Todo, async (sender, arg) =>
{
await RefreshCommand.ExecuteAsync();
});
这给我带来了错误任何帮助将不胜感激:)
This is giving me error Any Help will appreciated :)
推荐答案
要改善@ user2825546的答案,如果您只希望订阅来自视图模型的消息,则在发送讯息:
To improve the answer by @user2825546, if you wish to subscribe to only messages from your view-models, you need to specify the base class type when sending the message:
MessagingCenter.Subscribe<BaseViewModel, string>(this, "ShowError", (view, message) => { });
public class StartupViewModel : BaseViewModel
{
//Like this...
MessagingCenter.Send<BaseViewModel, string>(this, "ShowError", "Message");
//Or...
MessagingCenter.Send((BaseViewModel)this, "ShowError", "Message");
}
在测试时,我尝试以StartupViewModel
的形式发送消息,但是侦听器未收到消息.我猜想是这样,因为该类是从BaseViewModel
派生的.
When testing, I tried to send the message as StartupViewModel
, but the listener was not receiving the messages. I guessed that it would, since the class derives from the BaseViewModel
.
这篇关于如何以Xamarin形式使用消息传递中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!