Compartir a través de


How to: Create Locally Available Commands

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

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 within a module or multiple modules.

This topic describes how to create locally available commands.

Note

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 instructions on 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 and allows you to 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, as shown in the following code.

    void OnMyCommandExecute(MyCommandParameter parameter){
      // Implement business logic for myCommand.
    }
    
  2. Implement a method that contains the logic to determine whether the command can be executed, as illustrated 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. 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 invoking 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.

    <Button Name="MyCommandButton" Command="{Binding}">Execute MyCommand</Button>
    

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.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.