none
Implement ICommand.CanExecuteChanged in portable class library (PCL) RRS feed

  • Question

  • I'm trying to put my implementation of DelegateCommand (a.k.a. RelayCommand) into a portable class library (PCL) that targets both .NET and Metro-style UIs.

    The implementation of the CanExecuteChanged event is:

    publiceventEventHandlerCanExecuteChanged {

    add{ CommandManager.RequerySuggested += value; }

    remove{ CommandManager.RequerySuggested -= value; }

    }

    The CommandManager, however, does not seem to be available in this PCL.  So then what should the implementation of the CanExecuteChanged member look like?

    Thursday, March 15, 2012 9:36 PM

Answers

  • Metro style apps do not support CommandManager, hence why PCL lacks support for it. In fact, none of the XAML-based platforms (ie Metro, Phone or Silverlight) support CommandManager except WPF. There are two approaches that you can do with this (I've used both to varying level of success):

    1. Invent your own "CommandManager" abstraction, that in your WPF apps you implement and simply delegate onto the real CommandManager in WPF. In Metro, you could hook it up to a DispatcherTimer that fires every 100 or so ms and fires the RequerySuggested event, or perhaps only on idle. I show you how to implement platform-agnostic abstractions and have them implemented by multiple platforms over on this article: http://msdn.microsoft.com/en-us/magazine/hh852593.aspx
    2. Have you command fire the event when the model that it's applicable to changes. For example, on that same article, I have a button that is only enabled when the user has added something to the cart. In this case, I fire the event everytime the underlying 'cart' changes (see OnYourWayHome\OnYourWayHome.Core\ViewModels\ShoppingListViewModel.CheckoutCartCommand in the code attached to the article).

    David Kean


    Base Class Library Team (BCL) | My Blog: http://davesbox.com

    Thursday, March 22, 2012 4:38 PM
    Moderator

All replies