Export (0) Print
Expand All

How to: Create Locally Available Commands

Overview

Commands are a way to handle user interface (UI) actions. They provide a loosely coupled way to bind the user interface to the logic that performs the action. In this guidance, commands are classified as follows:

  • Locally available commands. These commands can be accessed by a limited set of tightly related elements, such as a view and its presentation model or presenter.
  • Globally available commands. These commands are available to all the elements in a module or in multiple modules.

This topic describes how to create locally available commands.

Ff921102.note(en-us,PandP.20).gifNote:
This topic assumes you are familiar with commands. For more information about commands, see the Commands technical concept.

Prerequisites

This topic assumes that you have a solution based on the Composite Application Library with a view. For information about how to create a solution based on the Composite Application Library, see How to: Create a Solution Using the Composite Application Library.

Steps

To create local commands, you typically define instances of the DelegateCommand<T> class. This class implements the System.Windows.Input.ICommand interface. By using this class, you can supply delegates for the Execute and CanExecute methods. This means that when the Execute or CanExecute methods are invoked on the command, the delegates you supplied are invoked. The following procedure describes how to create a locally available command.

To create a locally available command

  1. In the class where you want to define the command (this is typically a presenter class or a presentation model class), implement a method that contains the logic to be executed when the command is invoked. This method must return void and take a single parameter, this parameter can be of any type, such as a string or an integer. A sample implementation of this method, which has a parameter of MyCommandParameter type, is shown in the following code.
    void OnMyCommandExecute(MyCommandParameter parameter){
      // Implement business logic for myCommand.
    }
    
    Ff921102.note(en-us,PandP.20).gifNote:
    For information about creating a view with a presenter class, see How to: Create a View with a Presenter.

  2. Implement a method that contains the logic to determine whether the command can be executed, as shown in the following code. Note that the method signature must return bool and receive a single parameter (the parameter type must be the same of the parameter of the method you implemented in the previous step).
    bool OnMyCommandCanExecute(MyCommandParameter parameter){
      // Implement business logic for myCommand enablement.
      return true;
    }
    
  3. To the top of the class, add the following using statement.
    using Microsoft.Practices.Composite.Presentation.Commands;
    
  4. Add a variable definition to your class of type DelegateCommand<T>, where T is the type of the parameter the methods you implemented in the previous steps take. When you invoke the DelegateCommand<T>'s class constructor, pass handlers to the methods you defined in the previous steps, as shown in the following code.
    DelegateCommand<MyCommandParameter> myCommand = new DelegateCommand<MyCommandParameter>(OnMyCommandExecute, OnMyCommandCanExecute)
    

The following procedure describes how to add an invoker for the command you created in the previous procedure.

To add an invoker to a locally available command

  1. Pass the command instance to your view. The following code shows how to set a command instance as the DataContext of the view. By doing this, you can declaratively bind the command to a control in your view in the XAML code.
    view.DataContext = myCommand;
    
  2. Associate controls in your view with the command. The following code shows how to bind a button to the command in Windows Presentation Foundation (WPF).
    <Button Name="MyCommandButton" Command="{Binding}">Execute MyCommand</Button>
    

    The following code shows how to bind a button to the command in Silverlight using the Click.Command attached property.

    <Button Name="MyCommandButton" cal:Click.Command="{Binding}"/>Execute MyCommand</Button>
    

    To use the Click.Command attached property, make sure the following XML namespace is added.

    xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
    
    Ff921102.note(en-us,PandP.20).gifNote:
    Another approach to add an invoker without passing the command instance through the DataContext of the view, and therefore allowing you to use a model for the view, is wrapping the command inside a property in the model. For more information about this, see the section "To add an invoker to a globally available command" in How to: Create Globally Available Commands.

Outcome

You will have a locally available command with an invoker set up.

Next Steps

If you want to connect a locally available command to globally defined commands, see How to: Create Globally Available Commands.

More Information

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.


Home page on MSDN | Community site

Show:
© 2014 Microsoft