使用MVVM工具箱指示灯发送空的消息或通知 [英] Send a empty Message or Notification with MVVM toolkit light

查看:100
本文介绍了使用MVVM工具箱指示灯发送空的消息或通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM Light Toolkit.我找不到任何Messenger或Notification类的Ctor来发送空消息.

I'm using the MVVM Light Toolkit. I could not find any Ctor of Messenger or Notification class to send a empty message.

ViewModel1:

ViewModel1:

 private int _selectedWeeklyRotation;
    public int SelectedWeeklyRotation
    {
        get { return _selectedWeeklyRotation; }
        set
        { 
            if(_selectedWeeklyRotation == value)
                return;

            _selectedWeeklyRotation = value;
            this.OnPropertyChanged("SelectedWeeklyRotation");
            if(value > 1)
                Messenger.Default.Send();                     
        }
    }

ViewModel2:

ViewModel2:

Ctor:

Messenger.Default.Register(this, CreateAnotherTimeTable); 

private void CreateAnotherTimeTable()
{

}

我只需要向另一个ViewModel发送一个Notification,就根本不需要发送数据.

I just need to send a Notification to another ViewModel, no sending of data at all.

MVVM Light Toolkit库有可能吗?

Is that possible with MVVM Light Toolkit library?

推荐答案

您可以尝试发送带有字符串标签的简单消息,并通过匹配字符串标签来接收该消息.像这样:

You can try sending a simple message with a string tag and receive that message by matching the string tag. Something like this:

代码的发送方部分可能位于ViewModel1.cs

Sender portion of the code located possibly in something like ViewModel1.cs

Messenger.Default.Send<string>("Dummy text message", "String_ToHelpMatchTheMsg");

接收响应上述消息的代码的结尾部分,可能位于其他文件中,例如ViewModel2.cs
...

Receiving end portion of the code responding to that message above, possibly located in some other file, something like ViewModel2.cs
...

Messenger.Default.Register<string>(this, "String_ToHelpMatchTheMsg", executeThisFunction);

private void executeThisFunction(string strMsg)
{
   //your code would go here to run upon receiving the message
   // The following line will display: "Dummy text message" 
   System.Windows.Browser.HtmlPage.Window.Alert("msg passed: " + strMsg); 
}

请注意,您不必对随上面的消息传递代码传递的文本消息做任何事情.仅一部分代码向其他部分发送ping命令,以要求其他部分执行某些代码.重要的字符串是我在其中使用"String_ToHelpMatchTheMsg"的字符串,因为这是用于匹配发送方和接收方的密钥.就像创建自己的准事件一样,发送方法一旦运行,便会通知Register方法并触发其自己的函数以使其同时运行.

Please note that you dont have to do anything with the text message that is passed around with the messaging code above. Just one part of the code sending some ping to another part of the code to ask some other section to execute some code. The important string is the one where I used "String_ToHelpMatchTheMsg" because that is the key used to match the sender and the receiver. Almost like creating your own quasi-event, once the Send method runs, the Register method is notified and fire its own function to run also.

我将其与子窗口上的关闭"按钮一起使用以将其关闭.子窗口的视图"上的关闭"按钮绑定到其childWindowViewModel上的中继命令.该中继命令具有上面的代码,用于将消息发送到ParentViewModel. ParentViewModel上的Register部分通过触发一种方法来关闭该消息,该方法关闭了最初从该ParentViewModel实例化的ChildWindow.

I used this with a Close button on a Child Window to close it. The Close button on the View of the Child Window binds to a relay command on its childWindowViewModel. That relay command has the code above to send a message to the ParentViewModel. The Register portion on the ParentViewModel responds to that message by firing a method that closes the ChildWindow which was initially instantied from that parentViewModel.

一旦您更加熟悉消息传递,便可以使用更多属性,以便接收者可以回叫发送者以提供状态或一些数据.寻找Delegates和lambda函数来实现这一点.

Once you get more familiar with messaging, there are more attributes that you will be able to use so that the receiver can call back the sender to give a status or some data back. Look for Delegates and lambda function to achieve this.

所有这些避免将代码放在后面的代码中以关闭子窗口! :-) 视需要使用.

All this to avoid placing code in the code behind to close the child window! :-) Use as you see fit.

干杯. 马里奥(Mario)

Cheers. Mario

这篇关于使用MVVM工具箱指示灯发送空的消息或通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆