如何将消息发送到多个视图中的一个 [英] How to send message to one view of many
问题描述
我有一个应用程序,它基于相同的view和viewModel呈现许多view-viewModel对. (即,有很多view-> viewModel实例对)
I have an app that presents many view-viewModel pairs based on the same view and viewModel. (ie there are many view -> viewModel instance pairs)
我可以使用Messenger从viewModel发送/注册消息到视图,并且当我从视图发送消息时,所有视图都会对其进行处理(它们都为消息注册).
I can use the Messenger to send/register messages from viewModel to view, and when I send a message from a view, it is processed by all the views (They all register for the message).
如何使用Messenger将消息从viewModel发送到创建viewModel的特定视图? (所有视图都已注册该消息,但我只希望其中一个视图可以处理该消息)
How would I use the Messenger to send a message from the viewModel to the specific view that created the viewModel? (All the views have registered for the message, but I only want one of the views to process the message)
推荐答案
您可以在发送和注册消息时使用令牌.
You can use the Token while sending and registering Messaging.
假设您必须将Message从ViewModel传递到ABCView,然后可以像这样使用Messenging.
Suppose You have to pass Message From ViewModel to ABCView then you can use Messenging like this..
对于Ex,如果必须传递布尔值,请使用:
For Ex, if you have to pass boolean value then use:
Messenger.Defalut.Send<bool>(true,"ForAbcView");
在ABCView中,您可以这样注册:
And in ABCView you can register like this:
Messenger.Default.Register<bool>(this,"ForAbcView",(b)=>{ //Some Code });
使用令牌时,Messenger发送者将仅查找具有相同令牌的寄存器.它只会调用具有相同令牌的方法.
With using Token the Messenger Sender will only looks for the Register which will have same token. It only calls the method which will have same tocken.
这篇关于如何将消息发送到多个视图中的一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!