Intersoft ClientUI 8 > ClientUI Fundamentals > Commanding Overview > Commanding How-to Topics > How-to: Create Routed Commands In a Static Class |
This example shows how to create a RoutedCommand that defines the functions in your application.
Before a RoutedCommand can be consumed in your application, you need to create the RoutedCommand definition in your application code. A RoutedCommand is generally defined in a static class although it can be actually defined anywhere else in your application such as in the view's code behind or even in a view model. Defining RoutedCommand in a static class allows the command to be easily reused throughout your entire application.
A simple RoutedUICommand takes three parameters to construct: the text of the routed command, the name of the routed command, and the owner type of the routed command.
The following code example shows how to define routed commands in a static class. The editing commands example below is also used in several how-to topics related to commanding.
C# |
Copy Code
|
---|---|
using System.Windows.Documents; using System.Windows.Input; using Intersoft.Client.Framework.Input; namespace ClientUIApplication_Docs.Commands { public static class EditingCommands { private static readonly RoutedUICommand CutCommand = new RoutedUICommand("Cut", "Cut", typeof(EditingCommands)); private static readonly RoutedUICommand CopyCommand = new RoutedUICommand("Copy", "Copy", typeof(EditingCommands)); private static readonly RoutedUICommand PasteCommand = new RoutedUICommand("Paste", "Paste", typeof(EditingCommands)); public static RoutedUICommand Paste { get { return PasteCommand; } } public static RoutedUICommand Copy { get { return CopyCommand; } } public static RoutedUICommand Cut { get { return CutCommand; } } public static void Initialize() { // empty static class initializer } } } |
To use a RoutedCommand in your application, you reference to the public property that encapsulates the backing field such as shown in the following example:
XAML |
Copy Code
|
---|---|
<Intersoft:UXToolBarButton Name="btn_Cut" Command="Commands:EditingCommands.Cut" DisplayMode="Image" Icon="../Images/CutHS.png" ToolTipService.ToolTip="Cut"/> |