Odeslat dotazOdeslat dotaz
 

OdpovědětWPF commands

  • 4. listopadu 2009 16:08pymWPF Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Hello,

    This is a info questions, not a specific problem.
    I am trying to understand the Commands concept in WPF.
    I read some explanations and tried a few samples (menus and buttons bound to the same Command etc...)
    Yet, I fail to understand the whole idea.
    When are commands useful in real life applications of WPF.

    Thanks for your help!

Odpovědi

  • 4. listopadu 2009 16:21dmikon Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    One of the main benefits of commands is that they allow for loose coupling of components. With a command, you can invoke it through any means you want, with the owner of the command never knowing where the command was invoked from and by who. In comparison, with events you have to follow a specific event/delegate pattern for each event and have knowledge of the sender/receiver.

    For example, suppose you are implementing cut/paste functionality in an application. The requirements specify that the user can cut/paste through a menu, through some buttons, and through keyboard hotkeys (Ctrl+X, Ctrl+V for example). In this scenario, your cut/paste logic will be the same, but you will need many different event handlers for all of these options. With commands, you can bind to the same command, without creating specific event handlers; thus creating a more loose coupling between your cut/paste logic and your specific user interface.

    Naturally, that flows into the Model-View-ViewModel (MVVM) pattern. That is outside the scope of this post, but if you are interested in learning about it, you can start with this article on MVVM by Josh Smith:

    http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Všechny reakce

  • 4. listopadu 2009 16:21dmikon Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    One of the main benefits of commands is that they allow for loose coupling of components. With a command, you can invoke it through any means you want, with the owner of the command never knowing where the command was invoked from and by who. In comparison, with events you have to follow a specific event/delegate pattern for each event and have knowledge of the sender/receiver.

    For example, suppose you are implementing cut/paste functionality in an application. The requirements specify that the user can cut/paste through a menu, through some buttons, and through keyboard hotkeys (Ctrl+X, Ctrl+V for example). In this scenario, your cut/paste logic will be the same, but you will need many different event handlers for all of these options. With commands, you can bind to the same command, without creating specific event handlers; thus creating a more loose coupling between your cut/paste logic and your specific user interface.

    Naturally, that flows into the Model-View-ViewModel (MVVM) pattern. That is outside the scope of this post, but if you are interested in learning about it, you can start with this article on MVVM by Josh Smith:

    http://msdn.microsoft.com/en-us/magazine/dd419663.aspx