Saturday, January 3, 2015

MVVM Light RelayCommand - Broken & Fixed

If you've done much in the MVVM space, your probably familiar with the MVVM Light Toolkit created by Laurent Bugnion.  I've done a few projects recently using MVVM Light and I noticed that the relay command in WPF was not behaving as it normally did.  It was not magically handling the CanExecuteChanged event to update the status of the RelayCommand.  I decided to take the time to research the problem.  It didn't take long to find the following posted by Laurent on CodePlex for work item 7659

"WPF is the only XAML framework that uses the CommandManager to automagically raise the CanExecuteChanged event on ICommands. I never liked that approach, because of the "magic" part, but this is a "feature" of WPF and of course I have to support it. No question here.

In V5, I moved to portable class library for all the newest versions of the XAML frameworks, including WPF4.5. Unfortunately, there is no CommandManager in PCL, and I have to admit that I didn't realize that at first sight. So of course now the automagical part doesn't work anymore. Again, so sorry about that."

This seems to affect V5 - V5.01, with the fix coming in V5.02. Not long after he posted the fix, which is simply a change in namespace for the RelayCommand when using it with WPF.

"Change the namespace of the RelayCommand class from GalaSoft.MvvmLight.Command to GalaSoft.MvvmLight.CommandWpf."

This does indeed fix the issue :)

No comments:

Post a Comment