Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Registering a command container

If you don't use the extension methods below, you must register the command container inside the service locator and register the command inside the ICommandManager. To make this process easier, use a definition file and the code below.

Command definitions

To make it very easy to register new commands, Catel uses naming conventions and extension methods. The name of the command (for example, About must be a constant on the command definitions class). If the command definition also contains a <CommandName>InputGesture, in this case AboutInputGesture, it will use that input gesture as a default to register the command with.

Code Block
public static class Commands
{
	public static class Application
	{
		public const string Exit = "Application.Exit";
		public static readonly InputGesture ExitInputGesture = new InputGesture(Key.F4, ModifierKeys.Alt);

		public const string About = "Application.About";
		public static readonly InputGesture AboutInputGesture = new InputGesture(Key.F1);
	}
 
    public static class OtherPartOfApplication
    {
		public const string SomeCommand = "OtherPartOfApplication.SomeCommand";
		public static readonly InputGesture SomeCommandInputGesture = null;
    }
}
Info

It is recommended to keep a well formed structure for your command definitions to keep them manageable, even in very large applications

Registering the command container

Once you have the command container and the command definition (command name and the input gesture), it is time to register the command container:

Code Block
var commandManager = ServiceLocator.Default.ResolveType<ICommandManager>();
 
commandManager.CreateCommandWithGesture(typeof(Commands.Application), "About");

This will keep the command registration very readable and maintainable when using a lot of commands:

Code Block
var commandManager = ServiceLocator.Default.ResolveType<ICommandManager>();
 
commandManager.CreateCommandWithGesture(typeof(AppCommands.Application), "Exit");
commandManager.CreateCommandWithGesture(typeof(AppCommands.Application), "About");

commandManager.CreateCommandWithGesture(typeof(Commands.Project), "Open");
commandManager.CreateCommandWithGesture(typeof(Commands.Project), "Save");
commandManager.CreateCommandWithGesture(typeof(Commands.Project), "SaveAs");
commandManager.CreateCommandWithGesture(typeof(Commands.Project), "Refresh");

commandManager.CreateCommandWithGesture(typeof(AppCommands.Settings), "ToggleTooltips");
commandManager.CreateCommandWithGesture(typeof(AppCommands.Settings), "ToggleQuickFilters");

commandManager.CreateCommandWithGesture(typeof(ExtensibilityCommands.Application), "Extensions");
commandManager.CreateCommandWithGesture(typeof(ExtensibilityCommands.Application), "ExtensionsSettings");