locked
How to implement one class to inherit ICommand in release preview build? RRS feed

  • Question

  • Hi,

    http://www.codeproject.com/Articles/270415/Visual-Cplusplus-and-WinRT-Metro-Databinding-Basic
    This page have a simple sample to use ICommand.

    When I want implement one class to inherit ICommand
    It will fail

    How to implement one class to inherit ICommand in release preview build?
    Can give me some sample code?

    Thanks

    Thursday, July 5, 2012 4:14 AM

Answers

  • There isn't a Metro style sample demonstrating this yet. You can get an idea how this should work by reviewing the use of this interface in these articles and a sample project. I would expect a Metro style sample demonstrating this would surface in the near future.

    These are targeted for other platforms, but the usage pattern is the same.

    Building a Reusable ICommand implementation for Windows Phone Mango MVVM apps

    ICommand is like a chocolate cake

    Blend for Visual Studio Memory game tutorial files


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 8:20 AM
    Friday, July 6, 2012 7:13 AM
  • The original code was written on the dev preview, and I don't have a release preview lying around for easy access at the moment, but here's some code that will compile on the consumer preview. (and I assume the CP and RP are fairly close enough that it should compile on the RP too).

    delegate void ExecuteDelegate(Object^ parameter);

    delegate bool CanExecuteDelegate(Object^ parameter);

    ref class DelegateCommand sealed : public ICommand
    {
      ExecuteDelegate^ _executeDelegate;
      CanExecuteDelegate^ _canExecuteDelegate;

    public:
      DelegateCommand(ExecuteDelegate^ executeDelegate,
        CanExecuteDelegate^ canExecuteDelegate = nullptr)
      {
        _executeDelegate = executeDelegate;
        _canExecuteDelegate = canExecuteDelegate;
      }

      event EventHandler<Object^>^ CanExecuteChanged;

      void Execute(Object^ parameter)
      {
        _executeDelegate(parameter);
      }

      bool CanExecute(Object^ parameter)
      {
        return _canExecuteDelegate == nullptr || _canExecuteDelegate(parameter);
      }
    };

    Try this and see if it works for you as you expect it to.


    http://blog.voidnish.com

    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 8:20 AM
    Friday, July 6, 2012 11:42 AM

All replies

  • Hey Woody,

    I wrote that article - so I was wondering what you meant by "implement one class to inherit" ?

    The DelegateCommand class in the example is a class that implements ICommand. Are you getting compiler errors? That code was compiled on the dev preview, so perhaps namespaces or type names may have changed.


    http://blog.voidnish.com

    Thursday, July 5, 2012 5:12 PM
  • Hi Nishant,

    Yes, you got it.
    Because ICommand interface have some different in release preview.
    But, I don't know how to modify DelegateCommand class.
    So, I need some hint or sample code to fix this problem.

    Thanks

    Friday, July 6, 2012 2:54 AM
  • There isn't a Metro style sample demonstrating this yet. You can get an idea how this should work by reviewing the use of this interface in these articles and a sample project. I would expect a Metro style sample demonstrating this would surface in the near future.

    These are targeted for other platforms, but the usage pattern is the same.

    Building a Reusable ICommand implementation for Windows Phone Mango MVVM apps

    ICommand is like a chocolate cake

    Blend for Visual Studio Memory game tutorial files


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 8:20 AM
    Friday, July 6, 2012 7:13 AM
  • The original code was written on the dev preview, and I don't have a release preview lying around for easy access at the moment, but here's some code that will compile on the consumer preview. (and I assume the CP and RP are fairly close enough that it should compile on the RP too).

    delegate void ExecuteDelegate(Object^ parameter);

    delegate bool CanExecuteDelegate(Object^ parameter);

    ref class DelegateCommand sealed : public ICommand
    {
      ExecuteDelegate^ _executeDelegate;
      CanExecuteDelegate^ _canExecuteDelegate;

    public:
      DelegateCommand(ExecuteDelegate^ executeDelegate,
        CanExecuteDelegate^ canExecuteDelegate = nullptr)
      {
        _executeDelegate = executeDelegate;
        _canExecuteDelegate = canExecuteDelegate;
      }

      event EventHandler<Object^>^ CanExecuteChanged;

      void Execute(Object^ parameter)
      {
        _executeDelegate(parameter);
      }

      bool CanExecute(Object^ parameter)
      {
        return _canExecuteDelegate == nullptr || _canExecuteDelegate(parameter);
      }
    };

    Try this and see if it works for you as you expect it to.


    http://blog.voidnish.com

    • Marked as answer by Jesse Jiang Tuesday, July 17, 2012 8:20 AM
    Friday, July 6, 2012 11:42 AM