Prism: Using CompositeCommands, DelegateCommands and IActiveAware

Hi everybody,

Recently I found that these topics were often discussed in the Prism forum and, although there are samples already using these features provided by Prism as out of the box (e.g. Commanding Quickstart and Stock Trader Reference Implementation.) I thought that a small sample portraying all these functionalities together could become handy, especially one that shows the different behaviors of CompositeCommands (e.g. with and without the monitorCommandActivity parameter).

Introduction

As many of you might have read from the Prism documentation, CompositeCommands can be connected to several child commands so that when a CompositeCommand is invoked, its child commands are also invoked. Also, these commands support enablement, as they can listen to the CanExecuteChanged event of each one of its child commands. This way, when any call to the CanExecute method of the child commands returns false, the CompositeCommand’s CanExecute method will also return false and the corresponding invoker will be disabled.

Additionally, if its child commands implements the IActiveAware interface, the CompositeCommands provide the possibility to only execute and evaluate the corresponding Execute and CanExecute methods of the child commands that have its IsActive property set to true.

CompositeCommands can be configured to evaluate the active status of child commands (in addition to the CanExecute status) by specifying true to the monitorCommandActivity parameter in the constructor. In those cases DelegateCommands will become handy as they already implement the IActiveAware interface.

Basically, I created this sample with the purpose to highlight  the different possibilities that could be achieved when benefiting from these available functionalities.

Sample Application

pic

The Solution is a small application, that contains a shell project, one module and an infrastructure project.

There you can find that the module’s Initialize method registers two views in a region which is attached to a ListBox control in the shell. As their view model implements the IActiveAware interface, when one view is selected their active state is changed accordingly. Particularly, I decided to use a ListBox as the region placeholder, so it could be possible to appreciate the state of the different views displayed in the region simultaneously, although it could be easily replaced with any control that inherits from the Selector class.

Each of these views contain a pair of buttons which are bound to its corresponding DelegateCommand and they also contain a CheckBox which will allow to select the returned value of each CanExecute method in order to appreciate the different behaviors when those values are changed.

All these inner buttons’ DelegateCommands will be registered as child commands of the two CompositeCommands that are defined as static classes in the Infrastructure project so they could become globally available. One of them (GlobalCommands.MyGlobalCommand) sets its monitorCommandActivity parameter value to true and the other one (GlobalCommands.MyResetCommand) sets it to false.

You can download the sample application from my SkyDrive account, under the name “CompositeCommand&IActiveAware“.

Note that this code is provided “AS IS” with no warranties and confers no rights.

I hope you find it useful!



4 Comments

  • DineshRajan says:

    Good example and presented in neat manner. Thanks

  • leon says:

    this is absolutely amazingly easy-to-understand example, I love how it goes straight to nitty gritty

  • Shailender says:

    The explanation in the blog is very good. However am unable to download the sample. Error is “Something went wrong. Please try again”. Can you help please? Thank you.

  • Jon says:

    This sounds like it was a good example, but the link to the code appears to be broken. I’d love to be able to check out the code, if possible. Thanks so much for posting this!

Leave a Reply