Well, it has certainly been a while since I got to a good posting rhythm, I hope it changes soon because I have a series of posts in mind.
This isn’t a post about general help and description for PRISM, MVVM, or DelegateCommand. There are many resources about these already.
This post is to demonstrate how I managed to extend the DelegateCommand with some really cool features.
The DelegateCommand that ships with CAL expects an “execute method” and a “can execute method” arguments in its constructor.
This is nice, but I wanted something more.
I am involved in a big WPF development project where we have many commands that their state depends on several state-related parameters. I could just “push” it all in the “can execute” method, however, I don’t quite like it due to the following:
- The code is written very imperatively
- You need to reserve fields for every command to trigger its “CanExecuteChanged” when the appropriate state changes
- No encapsulation, the code is scattered around, code gets repeated
- Difficulty to maintain – you need to search for all related places when the requirement changes and synchronize everything appropriately.
- Plain and simple – not pretty
The goal is to make the Delegate Command to be attachable with numerous state bound objects that can alert it when it needs to be refreshed.
Starting Point – Attachable Components
I needed to make a common interface for the attachable components that could be attached to the DelegateCommand, as follows –
To make things a bit more easy for future classes, I created also a base class –
Step 2 – Enable INotifyCanExecuteChanged attachments to the DelegateCommand
At this point I got a delegate command that can get attachable notifiers. My command binds to the event of each notifier and raises its own CanExecuteChanged.
Additionally, there’s logic in the CanExecute to call the notifiers as well.
In Conclusion for this Part
This seals the first part of the Delegate Command Extensions.
The delegate command can get attachable notifiers which it bounds to and mange calls to them. This is the basic implementation that allows the developers to define a command and bind it to numerous state-bound notifiers.
I will show the continuation of the solution which turned our real nice, I recommend you stay tuned 🙂