开发者

What is the difference between WPF Command and Event?

What is the differen开发者_开发问答ce between WPF Command and Event?


Generally speaking you can do almost the same with events as with commands, it is just a different pattern of handling user interaction.

Commands in WPF allow you to move the implementation of a command handler to the buisness layer. Commands combine Enable state and executation, so everything is in place. Reade more by searching for the MVVM pattern.

Commands are more complex to implement at first, so if your application is small you should consider just sticking to events.


Commands are similar to Events except we can associate any number of UI Controls or Input Gestures to a command and bind that command to a handler that is executed when control are activated or gestures are performed.

Command also keep track weather or not they are available. If they are not available then all controls associated with that command are disabled.

The Code that executes when command is invoked is located in commands Execute event handler. The Code that determines is command can be or can not be invoked is located in commands CanExecute event handler.

WPF have some inbuilt Commands:

Command Class          | Example Commands
-----------------------------------------------
ApplicationCommands    | Close, Cut, Copy, Paste, Save, Print
NavigationCommands     | BrowseForward, BrowseBack, Zoom, Search
EditingCommands        | AlignXXX, MoveXXX, SelectXXX
MediaCommands          | Play, Pause, NextTrack, IncreaseVolume, Record, Stop


You can bind WPF command in the view (XAML) and receive the event raised. This way you do not have to use code behind which is a no-no in MVVM.

So the binding element is very important. But it also implements CanExecute which normally makes your control disabled if it returns false, e.g. if it is a button.


In events an action is tightly coupled with its source and can't be reused freely; Using commands you can easily maintain various actions in a single place and reuse them anywhere in application.

What makes commands different from a simple event handler attached to a button or a timer is that commands separate the semantics and the originator of an action from its logic. This allows for multiple and disparate sources to invoke the same command logic, and it allows the command logic to be customized for different targets.

taken from - Commanding Overview: http://msdn.microsoft.com/en-us/library/ms752308(v=VS.90).aspx

This article explains the concept of Commands and is must read before using commands.

This SO thread is also havign a lot of useful info. :

Custom WPF command pattern example


Roughly speaking, Command is encapsulation of Object (Button, Menu) Enable/Disable State and Action.

Limitation of Command:

  • Multicast (you can use multi binding but not so common and don't feel as nice as event multicast)
  • Need 2 step to light up the bulb. (If your button is always enable, then you just waste your code).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜