I am creating a WPF application and following the MVVM pattern. But while doing things I Worry about that, Is it according to MVVM or not? Please guide with these doubts.
Is is necessary to have a new ViewModel for each and every View? If not, then can creating a single MasterViewModel is violation of MVVM?
How will ViewModels communicate with each other?
MainWindow.xaml.cs
where I'm integrating all the Views, should have only the initialization of viewmodel and assigning the DataContext will there or I can put other codes also?
I am having my defined EventHandlers. Should I use them in ViewModel or outside of model-view-viewmodel?
MVVM separates your view (i.e. Activity s and Fragment s) from your business logic. MVVM is enough for small projects, but when your codebase becomes huge, your ViewModel s start bloating. Separating responsibilities becomes hard. MVVM with Clean Architecture is pretty good in such cases.
The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern.
Summary. The Model-View-ViewModel (MVVM) pattern helps to cleanly separate the business and presentation logic of an application from its user interface (UI).
Execution time in MVVM applications is faster with an average difference of 126.21 ms, while memory usage in MVP applications is lower with an average difference of 0.92 Mb.
You need some reading to do on MVVM. See the following questions:
Good examples of MVVM Template
Good Silverlight-MVVM Practice Example
MVVM Light Toolkit samples
For your questions:
Some people follow this rule. : One-ViewModel-Per-View See Rule #2 on this article.
It isn't absolutely necessary to follow this, but creating a MasterViewModel as using it everywhere, means you haven't understood MVVM yet.
If you are referring to encapsulating common bits, yet, maybe, the MVVM Light Toolkit Has a ViewModelBase class for encapsulating a few things there.
ViewModels won't communicate with each other, ViewModels will communicate with Views, and Views will communicate with other Views (and possibly instantiate ViewModels for them) some frameworks even have a loosely coupled way of doing this (ReactiveUI comes to mind)
When a View is called, you could instantiate its corresponding ViewModel and then set it to the DataContext. But, there are many different patterns to doing this. @Euphporic mentions in the comments the ViewModel-first where ViewModels create Views through Ioc. See Which came first- The View or ViewModel.
MVVM Light has a ViewModel locator which allows you to define a Views ViewModel statically inside XAML. It will automatically set when you create your view.
Not completely clear here, (a) If you have events from Buttons, Menu, (anything deriving from ButtonBase) you should implement these using the Command Pattern. MVVM Light has a nice RelayCommand<T>
that will help you here.
(b) Other events, MVVM Light has EventToCommand behavior but Laurent (the author) warns about this turning into an Anti Pattern. I think sometimes you could just use plain events in your code behind and then call your ViewModels from there (But hey I'm not an expert here)
You've asked a couple of questions all over the place. The problem maybe you do not understand the WHY about MVVM.
In plain terms MVVM allows you keep the right thing at the right place, your application logic/control in the ViewModels, and then with the power of Silverlight/WPF databinding you hook your ViewModels to your views.
As Laurent explains, sometimes it doesn't have to be all that complicated. You don't even need a framework all the time.
I highly recommend watching his MIX video titled - "Understanding the Model-View-ViewModel Pattern" from here:
http://live.visitmix.com/Archive#VideoList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With