Share via


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.

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, like a view and its presentation model or presenter.
  • Globally available commands. These commands are available to all the elements in a module or multiple modules.

This topic describes how to create globally 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 and command instances that you want to associate to a globally available command. 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. For instructions on how to create a locally available command, see How to: Create Locally Available Commands.

Steps

To create a globally available command, you typically create an instance of the CompositeCommand and expose it through a static class. The CompositeCommand class implements the System.Windows.Input.ICommand interface and supports programmatic registration of child commands. When the composite command is executed, all the child commands get executed. The following procedure explains how to create a globally available command.

To create a globally available command

  1. Create a static class to hold your command instance. You typically create this class inside a module or in the infrastructure project. If you want the command to be invoked only by views in a particular module, create the class in that module. Otherwise, if you want to expose the command to views from multiple modules, place the static class in the infrastructure project. For more information about the infrastructure project, see How to: Create a Solution Using the Composite Application Library.

  2. Add a public CompositeCommand instance to your static class, as shown in the following code.

    public static class GlobalCommands{    public static CompositeCommand MyCompositeCommand = new CompositeCommand();}
    
  3. In your module classes (typically presentation model classes), associate child commands to the globally available command. To do this, add the required using statements to your class and access the static instance of the command and register child commands with the RegisterCommand method, as shown in the following code.

    GlobalCommands.MyCompositeCommand.RegisterCommand(command1);GlobalCommands.MyCompositeCommand.RegisterCommand(command2);
    

    Note

    To increase the testability of your code, you can use a proxy class to access the globally available commands and mock that proxy class in your tests. For implementation details, see the Commanding QuickStart.

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

To add an invoker to a globally available command

  1. Associate controls in your view with the command. The following code shows how to bind a button to the command.

    <Button Name="MyCompositeCommandButton" Command="{x:Static local:GlobalCommands.MyCompositeCommand}">Execute My Composite Command </Button>
    

Outcome

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

Next Steps

If you want to create locally available commands to register with the globally command you created, see 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.