Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Create Globally 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 multiple modules.

This topic describes how to create globally available commands.

Ff648296.note(en-us,PandP.10).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 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 information about 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);
    
    Ff648296.note(en-us,PandP.10).gifNote:
    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 in Windows Presentation Foundation (WPF).

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

    Silverlight does not provide support for x:static, so perform the following steps to bind a button to a command in Silverlight:

    1. On the view's model, create a public property to obtain the command from the static class. This is shown in the following code.
      public ICommand MyCompositeCommand
      {
          get { return GlobalCommands.MyCompositeCommand; }
      }
      
    2. Typically, models are passed to the view through its DataContext (this is done at the view's code behind file). The following code shows how to set the model 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 = model;
      
    3. Ensure that the following XML namespace is added to root element of the view's XAML file.
      xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
      
    4. Bind a button to the command in Silverlight using the Click.Command attached property, as shown in the following code.
      <Button Name="MyCommandButton" cal:Click.Command="{Binding MyCompositeCommand}"/>Execute MyCommand</Button>
      
    Ff648296.note(en-us,PandP.10).gifNote:
    Another approach is to store the command as a resource inside the App.xaml file in the Application.Resources section. Then, in the view—which must be created after setting that resource—you can set cal: Click.Command="{Binding MyCompositeCommand, Source={StaticResource GlobalCommands}}" to add an invoker to the command.
    For more information about using commands in Silverlight, see Commands technical concept.

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.

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.