简体   繁体   English

Winforms中的事件与WPF中的命令有什么区别?

[英]What's difference between events in Winforms and commands in WPF?

In Winforms we (developers) handle user interactions via events. 在Winforms中,我们(开发人员)通过事件处理用户交互。 In WPF we got commands. 在WPF中我们得到了命令。

Questions : 问题:

  1. what's difference between events in Winforms and commands in WPF? Winforms中的事件与WPF中的命令有什么区别? Which approach we must use? 我们必须使用哪种方法? and when? 什么时候?

  2. what's difference between events in Winforms and routed events in WPF? Winforms中的事件与WPF中的路由事件有什么区别?

  1. Command represented by an object which could be serialized, passed over a process, whatever, let's say it is more "flexible". 命令由一个可以序列化的对象代表,通过一个进程传递,无论如何,让我们说它更“灵活”。

  2. Routed Events supports next strategies - direct, bubbling and tunneling , also using routed event you can indicate that event was processed by settign an appropriate flag in event arguments. 路由事件支持下一个策略 - 直接,冒泡和隧道 ,也使用路由事件,您可以指示事件由事件参数中的settign处理。

I'd highly recommend starting out by reading through MSDN's article on Routed Events , however from my point of view, the biggest difference is how they work 我强烈建议您阅读MSDN关于路由事件的文章 ,但从我的观点来看,最大的区别在于它们是如何工作的

Winforms lets you assign an method to an event handler, and whenever that control's event is raised, that handler gets run. Winforms允许您将方法分配给事件处理程序,并且无论何时引发该控件的事件,该处理程序都会运行。 You can actually do the same thing in WPF, or you can use a Routed Event. 你可以在WPF中实际做同样的事情,或者你可以使用路由事件。

In a Routed Event, an event is generated (such as a click event), and the any element in the Visual Tree can subscribe to do something during the Click event, and can mark the Event as handled or not. 在路由事件中,会生成一个事件(例如单击事件),并且Visual Tree中的任何元素都可以订阅在Click事件期间执行某些操作,并且可以将事件标记为已处理。

For example, suppose you have a Button containing a Border and an Image 例如,假设您有一个包含BorderImageButton

<Button>
    <Border>
        <Image>
    </Border>
</Button>

Clicking on the Image doesn't execute Button.ClickEvent , but instead simply raises a generic Click event which is processed by the Image first, then the Border , and then the Button . 单击Image不会执行Button.ClickEvent ,而是简单地引发一般的Click事件,该事件首先由Image处理,然后是Border ,然后是Button The event will actually continue up the VisualTree until it hits the Window object, unless one of the controls handling the event marks it as Handled 该事件实际上将继续向上运行VisualTree,直到它到达Window对象,除非处理该事件的其中一个控件将其标记为Handled

This type of Routed event is called a Bubbling event because the event travels, or "bubbles", up the Visual Tree. 这种类型的Routed事件称为Bubbling事件,因为事件在Visual Tree中传播或“冒泡”。 Another event type is Tunneling , where the event travels down the VisualTree, or Direct , where only the object that got clicked processes the event. 另一种事件类型是Tunneling ,事件沿着VisualTree或Direct传播,其中只有被点击的对象处理事件。

As for the difference between Routed Events and Commands , they provide two separate functions. 至于Routed EventsCommands之间的区别,它们提供两个单独的功能。 Events are built-in and are tied with the UI object that is handling the Event, while a Command is not tied to the UI object in any way, and provides built-in support for enabling/disabling controls. 事件是内置的,并且与处理事件的UI对象绑定,而Command不以任何方式绑定到UI对象,并提供对启用/禁用控件的内置支持。

For example, a Button with a Click event will pass the Button object into the Click event handler, while a Button with the Command property set will execute an unrelated command and automatically enable/disable the button depending on Command.CanExecute() 例如,具有Click事件的Button将Button对象传递给Click事件处理程序,而具有Command属性设置的Button将执行不相关的命令并根据Command.CanExecute()自动启用/禁用该按钮。

I use the MVVM design pattern, so almost always use Commands (if a control doesn't support the Command property, I use a custom Attached Command Behavior which lets me attach a command to almost any UI event), however I still use Events on occasion when I want to do something that affects the View only and doesn't do any business logic. 我使用MVVM设计模式,所以几乎总是使用命令(如果控件不支持Command属性,我使用自定义附加命令行为 ,它允许我将命令附加到几乎任何UI事件),但是我仍然使用事件当我想做一些只影响View的事情并且不做任何业务逻辑时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tableAdapter的命令和设计器中下面列出的查询之间有什么区别? - What is the difference between tableAdapter's commands and queries listed below it in designer? WPF DataGrid 的 Items 和 ItemsSource 属性之间有什么区别? - What's the difference between a WPF DataGrid's Items and ItemsSource properties? 添加新事件的以下代码之间有什么区别 - What's the difference between following code for adding new events WPF应用程序中资源和内容之间的区别是什么 - What's the difference between Resource and Content in a WPF application WPF中的类监听器和实例监听器之间有什么区别? - What's the difference between a class listener and an instance listener in WPF? 在WPF中,RenderTransform的null和Identity之间有什么区别? - In WPF, what's the difference between null and Identity for RenderTransform? WPF / UWP:DependencyObject的GetValue()和ReadLocalValue()之间有什么区别? - WPF/UWP: What is the difference between DependencyObject's GetValue() and ReadLocalValue()? WPF Dispatcher的InvokeAsync和BeginInvoke之间有什么区别 - What's the difference between InvokeAsync and BeginInvoke for WPF Dispatcher Winforms中的WPF事件 - WPF Events in Winforms 用于数据表的Winforms代码-WPF等价于什么? - Winforms code for datatable - what's the WPF equivalent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM