MVVM- View Model-View Model Communications

后端 未结 4 624
我在风中等你
我在风中等你 2020-12-14 03:37

How do I go about having two view models communicate with one another using MVVM Light. I know how to use the messenger class and register etc.. Here is my Scenario

4条回答
  •  生来不讨喜
    2020-12-14 04:27

    One thing you can try out is try to implement Dependency Inversion. Define an interface with some actions/contracts. Implement that interface on MainviewModel. Pass that interface as member variable to SettingsViewModel. So whenever settings view model has to notify something to main, it will use that interface. And additionally, other view models can use same strategy.

    public interface IMessenger
        {
          void NotifyAction();
        }

    public class MainViewModel:InotifyProprtyChanged,IMessenger
    {
     public void NotifyAction()
    {
    }
    }
    
    public class SettingsViewModel:INotifyPropertyChanged
    {
      public IMessenger Messenger{get;set;}
    
      public void SomeCommandExecutor()
      {
        if(Messenger!=null)
         {
           Messenger.NotifyAction();    
         }
    
      }
    }
    

提交回复
热议问题