Event isProgramThe part that affects the status change in the structure. Basically, the event is in. net is everywhere, and WPF and Silverlight are no exception. Events are used to notify users of input through the mouse and keyboard. but not limited to this. controls expose events that can be subscribed to in WPF and Silverlight, just like window forms and Asp.net. the difference lies in the implementation of these events and their behavior.
WPF and Silverlight use routed events instead of CLR events. the main reason for using different methods is that there may be multiple event subscribers in an element tree. therefore, CLR events directly call event subscriber for processing. routed events can be processed by any ancestor of event subscriber on the element tree. (popular saying: Event bubbling)
Command
Command pattern (gof) encapsulates the command function to separate callers and receivers.
Command helps us overcome the limitation of the event because we can trigger a command in the view and decide to accept it in the viewmodel. It can know what kind of processing should happen in the response.
In addition, the Multiple callers of the command ensure that we can use the interaction logic once in many different regions without repeating it.
Command pattern
Command pattern is a command interface similar to invoker, aggreger, and command interface in WPF and Silverlight. Understanding how entities collaborate will make it even more prominent that the default commands implement routed commands and routeduicommand. They are not fully suited to our ideas. Therefore, an alternative implementation is provided, which is more suitable for the mvvm architecture.
Icommandinterface
Public interface icommandsource
{
Icommand command {Get ;}
Object commandparameter {Get ;}
Iinputelement commandtarget {Get ;}
}
This design clearly shows that each class that executes the icommandsource interface can only expose one command attribute, explaining why most of the time trust events occur. If the event wants to expose multiple commands, the entire architecture will also be blocked.
Icommand Interface
Public interface icommand
{
Event eventhandler canexecutechanged;
Bool canexecute (object parameter );
Void execute (object parameter );
}
All commands think that they inherit from the icommand interface. The excute method is the core of the command mode and all its encapsulated commands are executed. We can provide any parameters required by execute, so that
Custom execution can be passed through context.
The canexcute method notifies the caller whether the command can be executed. In fact, it is automatically allowed and not allowed to be used by the caller. Take the previous example to exit the application. A long running process cannot be interrupted, so the application cannot exit. The application cannot exit now unless you provide a prompt to the user.
Routed command implementations
WPF and silvelight have two icommand implementations: routedcommand and routeduicommand.
The difference between routedcommand routeduicommand is that routedcommand has a text attribute.
The process that triggers a command.
Routedcommand
Routedcommand calls a commangd manage execued method.
The problem is: commandmanager and commandbinding are a set of non-direct, which are not in line with viewmodel requirements.
Relaycommand
Advantages:
1. Keep the controls on the view and the corresponding user input handler separate
2. relaycommand is used as an instance of the viewmodel class.
3. The relaycommand response is processed in the same class. If it is not direct, the handler will also access it through the strength attribute field.
4. canexecute and canexecutechanged are implemented and extended. Ensure that icommandsource is enabled and disabled.
5. Optional when canexecute parameter is set. No handler is specified. The default value of command is true.
6. canexecute will be processed by viewmodel
7. Maintain minimal additional dependencies
Example:
public class loginviewmodel
{
Public loginviewmodel ()
{
_ loginmodel = new loginmodel ();
_ logincommand = new relaycommand (Param => This. attemptlogin (), Param =>
This. canattemptlogin ();
}
Public String username
{
get;
set;
}
Public String password
{
get;
set;
}
Public icommand logincommand
{
Get
{
Return _ logincommand;
}
}
Private void attemptlogin ()
{
_ Loginmodel. login (username, password );
}
Private bool canattemptlogin ()
{
Return! String. isnullorwhitespace (username )&&! String. isnullorwhitespace (password );
}
Private relaycommand _ logincommand;
Private loginmodel _ loginmodel;
}