Send a empty Message or Notification with MVVM toolkit light

后端 未结 4 848
故里飘歌
故里飘歌 2021-01-22 00:14

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 _select         


        
相关标签:
4条回答
  • 2021-01-22 00:57

    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.

    0 讨论(0)
  • 2021-01-22 01:04

    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);
    
    0 讨论(0)
  • 2021-01-22 01:12

    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.

    0 讨论(0)
  • 2021-01-22 01:20

    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

    0 讨论(0)
提交回复
热议问题