MVVM Design Pattern Basics--icommand Interface

Source: Internet
Author: User

The command is an input mechanism in Windows Presentation Foundation (WPF) that provides input processing with a higher level of semantics than device input.
Commands have several uses:
The first purpose is to separate the semantics and the object that invokes the command from the logic that executes the command. This enables multiple, disparate sources to invoke the same command logic, and allows the command logic to be customized for different targets.
For example, editing operations such as copy, Cut, and paste that can be found in many applications can be called using different user actions if they are implemented using commands. The application may allow the user to cut the selected object or text by clicking a button, selecting a menu item, or using a key combination such as ctrl+x. By using commands, you can bind various types of user actions to the same logic.

Another use of the command is to indicate whether the operation is available.
Still using the Cut object or text as an example, the operation only makes sense if certain content is selected. If a user attempts to cut an object or text without selecting anything, nothing happens. To indicate this to the user, many applications disable buttons and menu items to let the user know if they can perform an action. Commands can be implemented by implementing the CanExecute method to indicate whether an operation can be performed. The button can subscribe to the Canexecutechanged event, and if CanExecute returns False, the button can be disabled, or if CanExecute returns True, the button can be enabled.

ICommand Interface
Define a command
Named control: System.Windows.Input
Assembly: PresentationCore (in PresentationCore.dll)
Syntax: public interface ICommand

ICommand Members

CanExecute method: Defines a method that determines whether this command can be executed in its current state.
Syntax: BOOL CanExecute (Object parameter)
When the Canexecutechanged event is raised, the command source calls the CanExecute method.

Execute method: Defines the method that is called when this command is called.
Syntax: void Execute (Object parameter)

Canexcutechanged event: Occurs when there is a change that affects whether the command is executed.
Syntax: Event EventHandler canexecutechanged
Typically, when the event occurs, the command source calls CanExecute to the command.

Practical Application:
In the ViewModel of MVVM, you need to inherit the ICommand interface and implement the functions in the interface.

 Public classrelaycommand:icommand{ReadOnlyAction _execute;ReadOnlypredicate _canexecute; Public Relaycommand(Action Execute): This(Execute,NULL)    {    } Public Relaycommand(Action Execute, predicate canexecute) {if(Execute = =NULL)Throw NewArgumentNullException ("Execute");        _execute = Execute;    _canexecute = CanExecute; } Public BOOL CanExecute(ObjectParameter) {return_canexecute = =NULL?true: _canexecute (parameter); } Public EventEventHandler canexecutechanged {add {commandmanager.requerysuggested + =value; } remove {commandmanager.requerysuggested-=value; }    } Public void Execute(ObjectParameter) {_execute (parameter); }}

Some notes in the appeal code:

    • You can use the action delegate to pass a method as an argument without explicitly declaring a custom delegate.
      The encapsulated method must correspond to the method signature defined by this delegate.
      In other words, the encapsulated method must have a parameter passed to it by value, and the value cannot be returned.

    • predicate delegate
      Represents a method that defines a set of conditions and determines whether the specified object conforms to those conditions.

    • The Add context keyword is used to define a custom event accessor that will be invoked when the client code subscribes to your event.
      If you provide a custom add accessor, you must also provide a remove accessor.

    • The Remove context keyword is used to define a custom event accessor that is invoked when the client code cancels the subscription event.
      If you provide a custom remove accessor, you must also provide an add accessor.

    • commandmanager.requerysuggested Events
      Occurs when Commandmanager detects conditions that may change the functionality of the command to be executed.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MVVM Design Pattern Basics--icommand Interface

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.