使用 MVVM 工具包 light 发送空消息或通知

发布于 2024-09-03 07:10:18 字数 787 浏览 1 评论 0原文

我正在使用 MVVM Light 工具包。我找不到任何 Messenger 或通知类的 Ctor 来发送空消息。

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:

Ctor:

Messenger.Default.Register(this, CreateAnotherTimeTable); 

private void CreateAnotherTimeTable()
{

}

我只需要向另一个ViewModel发送通知,根本不发送数据。

MVVM Light Toolkit 库可以实现这一点吗?

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

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:

Ctor:

Messenger.Default.Register(this, CreateAnotherTimeTable); 

private void CreateAnotherTimeTable()
{

}

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

Is that possible with MVVM Light Toolkit library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

煮酒 2024-09-10 07:10:18

除非我误解了什么,否则您不能通过 Messenger 创建并发送自定义“信号消息”类型来完成此操作吗?

public class WeeklyRotationSignal {}

Messenger.Default.Send(new WeeklyRotationSignal());

然后在另一个视图模型中注册:

Messenger.Default.Register<WeeklyRotationSignal>(this, msg => doWork);

Unless I'm misunderstanding something, couldn't you accomplish this by creating and sending a custom "signal message" type via the Messenger?

public class WeeklyRotationSignal {}

Messenger.Default.Send(new WeeklyRotationSignal());

Then register to that in another view model:

Messenger.Default.Register<WeeklyRotationSignal>(this, msg => doWork);
风启觞 2024-09-10 07:10:18

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

代码的发送部分可能位于 ViewModel1.cs 之类的文件中

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

响应上述消息的代码的接收端部分可能位于其他文件中,例如 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”的字符串,因为这是用于匹配发送者和接收者的密钥。几乎就像创建您自己的准事件一样,一旦 Send 方法运行,Register 方法就会收到通知并触发其自己的函数也运行。

我将其与子窗口上的“关闭”按钮一起使用来关闭它。子窗口视图上的关闭按钮绑定到其 childWindowViewModel 上的中继命令。该中继命令包含上面的代码,用于向 ParentViewModel 发送消息。 ParentViewModel 上的 Register 部分通过触发关闭最初从该 ParentViewModel 实例化的 ChildWindow 的方法来响应该消息。

一旦您更加熟悉消息传递,您将能够使用更多属性,以便接收者可以回调发送者以返回状态或一些数据。寻找委托和 lambda 函数来实现这一点。

所有这一切都是为了避免将代码放在后面的代码中来关闭子窗口! :-)
按照您认为合适的方式使用。

干杯。
马里奥

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

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

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

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); 
}

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.

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.

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.

Cheers.
Mario

黑色毁心梦 2024-09-10 07:10:18

确实没有办法实现这一点,并且在某种程度上违背了信使类的目的。我不想写一篇你做错了的帖子,但我觉得我被困住了。信使类的工作方式是,两方都订阅相同的概念,即观察者模型。如果没有类似的概念或消息,实际上就没有办法将这两个对象联系在一起。通用消息(无论是简单字符串还是自定义消息)充当订阅类和发布类的交汇点。

如果 ViewModel 发布知道其尝试发送给它的 ViewModel 的类型,则可以...

Messenger.Default.Send<Type>(typeof(ViewModelToSendTo);

这将充当一个非常简单的交互点,您也不必创建自定义类。一些纯粹主义者可能对这种方法有疑问,因为它将发布类与订阅者耦合在一起。

There really isn't a way to accomplish this and in someways defies the point of the messenger class. I didn't want to write a your doing it wrong post, but I feel I am stuck. The way the messenger class works is that you have two parties that both subscribe to the same concept, its an observer model. Without that similar concept or message there really isn't a way to tie the two objects together. The generic message whether a simple string or custom message act as the meeting point of the Subscribing and Publishing classes.

If the ViewModel publishing knows the type of ViewModel its trying to Send to it could...

Messenger.Default.Send<Type>(typeof(ViewModelToSendTo);

This would act as a very simple interaction point, you also wouldn't have to create a custom class. Some purist may have an issue with this approach as it couples the publishing class to the subscriber.

情栀口红 2024-09-10 07:10:18

我认为这是不可能的,坦率地说,我不认为传达这种信息有什么意义。您也可以发送字符串“SelectedWeeklyRotation”。当您增加应用程序中广播消息和接收器的数量时,拥有某种具有某种含义的空消息似乎很奇怪。

在我使用的 MVVM Light 版本中,甚至无法发送空消息。

不过,我确实在 ViewModelBase 中看到了一个方法:

// Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);

您可能对此感兴趣。

I don't think that it is possible and frankly I don't see the point of having that kind of message. You could just as well send a string "SelectedWeeklyRotation". It seems strange to have an empty message that has some kind of meaning as you increase the number of broadcast messages - and receivers in your application.

In the version of MVVM Light that I'm using it is not even possible to send an empty message.

However I did see a method in the ViewModelBase that is :

// Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);

This might be of interest for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文