I'm pretty new in WPF programming environment. I'm trying to write a program out using MVVM design pattern.
I've did some studies and read up some articles related to it and many of a time I came across this thing called
ViewModelBase
I know what it is.. But may I know specifically where should I begin with to be able to write out my own ViewModelBase? Like... Really understanding what's happening without getting too complicated. Thank you :)
It's worth nothing to use MVVM frameworks if you don't know what's going on inside.
So let's go step by step and build your own ViewModelBase class.
ViewModelBase is class common for all your viewmodels. Let's move all common logic to this class.
Your ViewModels should implement
INotifyPropertyChanged
(do you understand why?)the
[CallerMemberName]
attribute is not required, but it will allow you to write:OnPropertyChanged();
instead ofOnPropertyChanged("SomeProperty");
, so you will avoid string constant in your code. Example:Please note, that
OnPropertyChanged(() => SomeProperty)
is no more recommended, since we havenameof
operator in C# 6.It's common practice to implement properties that calls PropertyChanged like this:
Let's define SetProperty in your viewmodelbase:
It simply fires
PropertyChanged
event when value of the property changes and return true. it does not fire the event when the value has not changed and return false. The basic idea is, thatSetProperty
method is virtual and you can extend it in more concrete class, e.g to trigger validation, or by callingPropertyChanging
event.This is pretty it. This is all your ViewModelBase should contain at this stage. The rest depends on your project. For example your app uses page base navigation, and you have written your own NavigationService for handling navigation from ViewModel. So you can add NavigationSerivce property to your ViewModelBase class, so you will have access to it from all your viewmodels, if you want.
in order to gain more reusability and keep SRP, I have class called BindableBase which is pretty much the implementation of INotifyPropertyChanged as we have done here. I reuse this class in every WPF/UWP/Silverligt/WindowsPhone solution, because it's universal.
Then I in each project create custom ViewModelBase class derived from BindableBase:
if I have app, that uses page based navigation I also specify base class for page viewmodels.
I could have another class for dialogs:
I like this BaseVewModel it gives a nice clean style to your view models. Check out the various 'before' and 'after' comparisons. Of course, nothing is mandatory - if you don't like a feature that the BaseViewModel provides then don't use it. Or modify it because you have the source code. In particular note that there are three different ways to implement properties with change notification - choose the level of sophistication that you understand/feel comfortable with.
In most MVVM frameworks, the base ViewModel classes actually contain very little code - usually just an implementation of INotifyPropertyChanged and some helper functions.
Take a look at the source code for MVVM Light's ViewModelBase and ObservableObject classes. ObservableObject is mostly the INotifyPropertyChanged implementation - using a lambda expression rather than "magic strings" for the property name. ViewModelBase extends ObservableObject and is mostly a utility method to determine if you're running inside the Visual Studio designer
The below class can be used as View model base in WPF projects:
And a example of ViewModel class is:
There is a good discussion here: https://codereview.stackexchange.com/q/13823 on the subject. Takes a good approach using expressions so that you get type safety when raising the property changed notifications.
You have some nuget package to implement MVVM
For me the easier for a beginner is MVVM light because it provide some code sample.
So the better is to install this nuget package, have a look about the generated code and back to us for more explanations if you need.