WPF Learning 1: Commands

Source: Internet
Author: User

1. Traditional events and commands:

For example, an application can execute the printdocument () method in two ways; 1. Use a button; 2. Use a shortcut key (B );

In traditional events, two events are created for both the button event and the shortcut key method (although the same printdocument () method is used ), you also need to determine whether the shortcut keys need to be used (the reason is that the button activation status and the shortcut key activation status are not synchronized );

In command cases, two features are added: 1. Delegate an event to an appropriate command; 2. Enable the control and synchronize the status of the corresponding command;

2. four important elements of commands

1. commands; 2. Command binding; 3. Command source; 4. Command target;

3. The core of a command is the system. Window. Input. icommand interface, which defines the operating principle of the command.

Public interface icommand

{

Void execute (object parameter); // finally triggers an event to be processed elsewhere in the application;

Bool canexecute (object parameter); // return the status of the command. If the command is available, true is returned. If the command is unavailable, false is returned;

Event eventhandler canexecutechanged; // This event is triggered when the command status changes.

// Note: commands are available and command sources are available. commands are unavailable and command sources are unavailable;

}

Iv. routedcommand class

The system. Windows. Input. routedcommand class is the only class in WPF that implements the icommand interface. In other words, the WPF command is an instance of this class;

An important concept of the WPF command model is that the routedcommand class does not contain any application logic. It only represents a command, which means that each routedcommand object has the same function;

To support routing events, this class implements the icommand interface and adds different versions of the icommand interface method. The most obvious change is that execute () and canexcute () use an additional parameter;

Public void execute (object parameter, iinputelement target );

Public bool canexecute (object parameter, iinputelement target );

The target parameter is the element that starts to process events. The event starts from the target element and then bubbles to the high-level container until the event is processed;

V. routeduicommand class

The routeduicommand class inherits from the routedcommand class, which is used for commands with text. These texts are displayed in some places on the user interface, and only one attribute text is added. The advantage is that the text can be localized;

6. Problems solved by commands: 1. Command tracking; 2. "unrecoverable commands"; 3. commands in different states and modes;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.