c# - Is this the correct way to use the MVVM Light Messenger class -


I started with a way to implement navigation in an MVVM environment. Then started reading about messenger class and how useful it would be to communicate between ViewModels. I had to apply the same navigation concept set out using messenger class and came up with this:

MainViewModel:

  public sealed class MainViewModel: ViewModelBase {public MainViewModel () {PageViewModels.Add (new page 1ViewModel ()); PageViewModels.Add (new page2ViewModel ()); Messenger.Default.Register & lt; PageNavigationMessage & gt; (This, (pageViewModel) = & gt; {switch (pageViewModel.CurrentViewModel.Name) {case "Page 1": CurrentPageViewModel = PageViewModels [0]; break; case "Page 2": CurrentPageViewModel = PageViewModels [1]; break; }}); } Private listing & lt; IPageViewModel & gt; _pageViewModels; Public listing & lt; IPageViewModel & gt; PageViewModels {Get (_pageViewModels == empty) _pageViewModels = New list & lt; IPageViewModel & gt; (); Return _pageViewModels;}} Private IPageViewModel _currentPageViewModel; Public IPViewViewModel CurrentPageViewModel {back {_currentPageViewModel;} set {if (_currentPageViewModel! = Value) {_currentPageViewModel = value; RaisePropertyChanged ("CurrentPageViewModel")}}} Private ICommand _changePageCommand; Public ICommand ChangePageCommand {if (_changePageCommand == faucet) {_changePageCommand = Get new RelayCommand & lt;; IPageViewModel & gt; (PN = & gt; Messenger.Default.Send (new PageNavigationMessage {CurrentViewModel = P N}));} return _changePageCommand;}}  

this is the page to see the 1 and page 2 My offers have named property content button is. So Page 1 will produce buttons and page 2 button.

I will only Page1ViewModel because one page name in addition to the property string is quite similar. < / P>

  public class page 1ViewModel: ViewModelBase, IPageViewModel {public string name {get {return "Page1"; }}}  

Here I get a basic PageNavigationMessage class implemented:

  public class PageNavigationMessage {public IPageViewModel CurrentViewModel {; Set; }}  

It works very well, but I was thinking that this is the correct way to implement the Messenger category in the MVVM Light Toolkit.

I have mentioned a detailed post and Rachel Leam's blog post, for example, which is referred to above.

Any comments, suggestions, etc. are highly appreciated.

This is very much the same way we do in our application.

Anything we do for Messenger too:

  • Create a normal message: Make a square, all of which have different messages since most of them are just an announcement with the legacy of the message That is, a class will be a mess for each message.
  • If your class has registers for more than one or more messages, then we can easily add / delete the message registration method to easily add / delete
  • If your message If the argument is more than one line or two, then we generally put them in an additional method, for example your example might be:

  public MainViewModel () {Messenger.Default.Register & lt; PageNavigationMessage & gt; (This, SetPageViewModel); } Private void SetPageViewModel (IPageViewmodel selectedVm) {switch (selectedVm.CurrentViewModel.Name) {case "Page 1": CurrentPageViewModel = PageViewModels [0]; break; Case "Page2": Current PageModel = Pageview Model [1]; break; }}  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -