WPF-15: command-1

From "Introduction to WPF" (Liu Tiemeng) reading notes

The command system of WPF consists of several basic elements:

a) Command: WPF's command actually implements the ICommand interface class;

b) Command Source (Command Source): implements the ICommandSource interface class;

c) Command target: implements the IInputElent interface class;

d) Command Binding: Responsible for associating some peripheral logic with commands.

The use of commands can be divided into:

i) create command;

ii) declare the command instance;

iii) specify the command source;

iv) Specify the command target: If the command target is not specified for the command source, the WPF system considers that the object that currently has the focus is the command target;

v) Set command association:

Example: Define a command and use Button to send the command. When the command is sent to the TextBox, the TextBox will be cleared. If there is no text in the TextBox, the command cannot be sent.

Add InitializeCommand() in the constructor form function:

private void InitializeComand()
{
    //Assign the command to the command source (sender) and assign the shortcut key
    this.button1.Command=this.clearCmd;
    this.clearCmd.InputGestures.Add(new KeyGesture(Key.C,ModifierKeys.Alt));
    //Specify the command target
    this.button1.CommandTarget=this.textBoxA;
    //create command association
    CommandBinding cb=new CommandBinding();
    cb.Command=this.clearCmd;
    cb.CanExecute+=new CanExecuteRoutedEventHandler(cb_CanExecute);
    cb.Executed+=new ExecutedRoutedEventHandler(cb_Executed);
    / / Place the command association on the peripheral control
    this.stackPanel.CommandBindings.Add(cb);

Declare and define the command in the form:

private RoutedCommand clearCmd=new RoutedCommand("clear",typeof(Window1));

Defined in the form: This method is called when the probe command can be executed

void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if(string.IsNullOrEmpty(this.textBoxA.Text))
    {e.CanExecute=false;}
    else
    {e.CanExecute=true;}
    //Avoid continuing to pass up and reduce program performance
    e.Handled=true;
}

This method is called when the command is sent to the target

void cb_Executed(object sender, ExecutedRoutedEventArgs e)
{
    this.textBoxA.Clear();
    //Avoid continuing to pass up and reduce program performance
    e.Hanldled=true;
}

Notice:

1) Using commands can avoid writing code to determine whether the Button is available and adding shortcut keys;

2) RoutedCommand is a class that has nothing to do with business logic. It is only responsible for running errands in the program and does not do anything with the command target;

3) Because the firing frequency of the CanExecute event is relatively high, in order to avoid performance degradation, it is recommended to set e.Handled to true after processing;

4) CommandBinding must be set on the peripheral control of the command target, otherwise routing events such as CanExecute and Executed cannot be captured.


Commands have the characteristic of "declare once, use everywhere".

WPF contains some convenient command libraries, such as: ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommands, MediaCommands, EditingCommands, they are all static classes, and commands are exposed in a singleton mode using the static read-only properties of these classes.

Command Parameters: CommandParameter--The distinction of the actual message passed.

By using Binding, you can call various commands of the control when there is only one Command.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325659637&siteId=291194637