Commands, Input and the WPF - Defining commands
(Page 2 of 4 )
Example 4-17 shows how to define a custom command. WPF uses object instances to establish the identity of commands—if you were to create a second command of the same name, it would not be treated as the same command. Because commands are identified by their command objects rather than their names, commands are usually put in public static fields or properties.
Example 4-17. Creating a custom command
...
using System.Windows.Input;
namespace MyNamespace {
public class MyAppCommands {
public static RoutedUICommand AddToBasketCommand;
static MyAppCommands() {
InputGestureCollection addToBasketInputs =
new InputGestureCollection();
addToBasketInputs.Add(new KeyGesture(
Key.B, ModifierKeys.Control|ModifierKeys.Shift));
AddToBasketCommand = new RoutedUICommand(
"Add to Basket", "AddToBasket",
typeof(MyAppCommands), addToBasketInputs);
}
}
}
The first RoutedUICommand constructor parameter is the name as it should appear in the user interface. In a localizable application, you would use a mechanism such as the .NET class library’s ResourceManager to retrieve a localized string rather than hardcoding it. The second constructor parameter is the internal name of the command as used from code—this should match the name of the field in which the command is stored, with the command suffix removed.
As with the built-in commands, your application command doesn’t do anything on its own. It’s just an identifier. You will need to supply command bindings to implement the functionality. You will also typically want to associate the command with menu items or buttons.
Using commands in XAML
Example 4-18 shows a Button associated with the standard Copy command.
Example 4-18. Invoking a command with a Button
<Button Command="Copy">Copy</Button>
Because this example uses a standard command from theApplicationCommandsclass, we can use this short form syntax, specifying nothing but the command name.
However, for commands not defined by the classes in Table 4-4, a little more information is required. The full syntax for a command attribute in XAML is:
[[xmlNamespacePrefix:]ClassName.] EventName
If only the event name is present, the event is presumed to be one of the standard ones. For example,Undois shorthand forApplicationCommands.Undo. Otherwise, you must also supply a class name and possibly a namespace prefix. The namespace prefix is required if you are using either custom commands, or commands defined by some third-party component. This is used in conjunction with a suitable XML namespace declaration to make external types available in a XAML file. (See Appendix A for more information on clr-namespaceXML namespaces.)
Example 4-19 shows the use of the command-name syntax with all the parts present. The value ofm:MyAppCommands.AddToBasketCommandmeans that the command in question is defined in theMyNamespace.MyAppCommandsclass in theMyLib component, and is stored in a field calledAddToBasketCommand.
Example 4-19. Using a custom command in XAML
<Window xmlns:m="clr-namespace:MyNamespace;assembly=MyLib" ...>
...
<Button Command="m:MyAppCommands.AddToBasketCommand"> Add to Basket</Button>
...
Because commands represent the actions performed at the user’s request, it’s likely that some commands will be invoked very frequently. It is helpful to provide keyboard shortcuts for these commands in order to streamline your application for expert users. For this, we turn to input bindings.
Next: Input Bindings >>
More .NET Articles
More By O'Reilly Media
|
This article is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Check it out today at your favorite bookstore. Buy this book now.
|
|