When and where to unregister messenger with mvvmli

2019-04-11 01:36发布

I build a project with mvvmlight. Using Messenger send message between VMs or VM and View. Now, I wonder that when and where to unregister messenger. Could u help me?

Edited:

This is my scene:

There are a MainWindow and a ChildWindow, the ChildWindow (include a ContentControl consist of one of two different UserControl), change the ContenControl via a SwitchButton in ChildWindow. Every window or UserControl corresponding to a ViewModel.

enter image description here

  1. MainViewModel sends a message to MainWindow to open the ChildWindow;
  2. MainWindow opens the ChildWindow (firstly use UserControlView1, don't use UserControlView2), and send a message to UserControl1ViewModel do something;
  3. If user click the CancelButton, ChildWindowViewModel sends a message to ChildWindow to close;
  4. If ChildWindowViewModel fire UnloadedCommand, ChildWindow sends a message to each of UserControlViewmodel to clean up something.

The operation of register and unregister messenger put in the constructor and the destructor of the View or the ViewModel, or put in the LoadedCommand and UnloadedCommand's delegate methods? View's Loaded and Unloaded's command binding to LoadedCommand and UnloadedCommand.

0条回答
登录 后发表回答