20

I'm getting started with Metro style applications (I know we're not supposed to call it Metro, but I can never remember what it's supposed to be called...), and I'm implementing a DelegateCommand class for use in MVVM. In WPF, the ICommand.CanExecuteChanged event is typically implemented like this:

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

But the CommandManager class does not exist in WinRT... Is there something else instead? Or are we supposed to explicitly trigger this event ourselves?

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758

2 Answers2

14

In WinRT, you must update/raise CanExecuteChanged manually. There is no CommandManager to do this globally. You could look at this as a pain in the neck, or a serious performance boost now that CanExecute is not called constantly. It does mean you have to think about cascading property changes where before you did not have to. But this is how it is. Manual.

public void RaiseCanExecuteChanged()
{
    if (CanExecuteChanged != null)
        CanExecuteChanged(this, EventArgs.Empty);
}
Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • Thanks for your answer. That's what I eventually figured out on my own. I guess there are good reasons for not including the CommandManager in WinRT, but it means I have to change the way I work with commands... – Thomas Levesque Sep 25 '12 at 17:30
  • 1
    Don't assume there is a good reason for not including CommandManager. Since this is round one of WinRT, some choices were made just from a do-we-have-enough-time point of view. Know what I mean? ;) – Jerry Nixon Sep 25 '12 at 21:27
1

With WPF it is mandatory that you implement ICommand.CanExecuteChanged as you have described. However, for Silverlight, this is not the case. See this related question:

WPF CommandParameter binding not updating

My guess is that in WinRT, as in Silverlight, the CommandManager is not necessary.

See the related discussion here:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/477cdd19-ee88-4746-97fe-59b8dbd44e0a/

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232