简体   繁体   English

为什么C#没有像VB那样的Handles子句?

[英]Why C# does not have Handles clause like VB?

In VB, we have the Handles clause, that allows add a Handler to an event of a control without putting it into the xaml file (directly into the VB file). 在VB中,我们具有Handles子句,该子句允许将Handler添加到控件的事件中,而无需将其放入xaml文件(直接放入VB文件中)。

xaml: xaml:

<Button x:Name="myButton" />

VB: VB:

Private Sub Button_Click() Handles myButton.Click

End Sub

One good thing that can be done using this is the possibility to use the Visual Studio dropdowns to add events automatically without the need to go to the xaml file and change it. 使用此功能可以完成的一件好事是可以使用Visual Studio下拉列表自动添加事件,而无需转到xaml文件并进行更改。 Read this question (and the answer) to better understand what I'm talking about: 阅读此问题(和答案)以更好地理解我在说什么:

Visual studio 2010 showing available events from code behind Visual Studio 2010从代码后面显示可用事件

The answer of that question doesn't specify why C# does not have this feature inside Visual Studio, but it's clear for me: C# does not have this feature because it uses the Handles clause to add the event on CodeBehind. 这个问题的答案没有指定为什么C#在Visual Studio中没有此功能,但对我来说很清楚:C#不具有此功能,因为它使用Handles子句在CodeBehind上添加事件。

I know that we can make use of the += and add the event manually on the constructor, below InitializeComponent (that is almost the same thing), but VB also have the AddHandler that can add events on the constructor (and in other places), and it's not automatic and less reliable (for me) than the Handles clause. 我知道我们可以利用+ =并在InitializeComponent下方的构造函数上手动添加事件(这几乎是同一件事),但是VB还具有可以在构造函数上(以及其他位置)添加事件的AddHandler。 ,并且它不是自动的,并且不如Handles子句可靠(对我而言)。

My question is: 我的问题是:

Why it's never implemented? 为什么从未实施? It's non-reliable? 不可靠吗? Non-secure? 不安全? There's any workaround? 有什么解决方法吗?

It was never implemented because no one at Microsoft thought it was useful enough to justify the effort. 从来没有实施过它,因为Microsoft没人认为它足够有用以证明这一努力是正确的。 The exact reasons why can only be answered by someone on the C# team. 只能由C#团队中的某人回答的确切原因。 And while it's true that C# and VB have made an effort to synchronize their features, that doesn't mean they're going to retroactively introduce all VB-specific features in C# or vice versa. 尽管C#和VB确实努力同步了它们的功能,但这并不意味着他们将在C#中追溯引入所有VB特定的功能,反之亦然。 (Note that the Handles clause has always existed in VB.NET) (请注意,VB.NET中始终存在Handles子句)

However, one can speculate based on VB's history as a language why it might have been introduced. 但是,人们可以基于VB的历史来推测为什么要引入VB。 Namely, that's how VB events pre-NET always worked, so VB developers would probably be used to it. 即,这就是pre-NET上VB事件始终起作用的方式,因此VB开发人员可能会习惯于它。

In traditional VB, events were wired up to objects by name. 在传统的VB中,事件按名称连接到对象。 If you have a Form and you define a subroutine named Form_Load , it will run as your form's Load event. 如果您有一个Form并且定义了一个名为Form_Load的子例程,它将作为表单的Load事件运行。 This tradition carried over into ASP, and still lingers as the AutoEventWireup configuration option. 这一传统一直延续到ASP中,并且仍然保留为AutoEventWireup配置选项。 VB developers were used to the language knowing what method to run for which events without having to "explain it" to the compiler. VB开发人员习惯了这种语言,无需知道向编译器“解释”哪种方法可以针对哪些事件运行。

In .NET languages, events are just a particular kind of property with a special type (a delegate type) that have to be assigned like any other property. 在.NET语言中,事件只是具有特殊类型(委托类型)的一种特殊属性,必须像其他任何属性一样进行分配。 In order to allow VB developers to transition easily into VB.NET, though, you would ideally give them an easy way to do so without having to learn about events and delegates and handlers (at least not immediately). 但是,为了使VB开发人员可以轻松地过渡到VB.NET,理想情况下,您最好为他们提供一种简便的方法,而不必了解事件以及委托和处理程序(至少不是立即进行)。 The Handles keyword accomplishes this -- you just tack on Handles Load onto your Form_Load sub and it becomes an event handler. Handles关键字可完成此操作-您只需将Handles Load Form_LoadForm_Load子项上,它将成为事件处理程序。

C#, on the other hand, has no legacy behavior that it needed to maintain. 另一方面,C#没有需要维护的遗留行为。 The target audience for C# consisted of people from many languages, most of which had no built-in concept of events and certainly did not have the auto-wireup behavior of VB. C#的目标受众包括来自多种语言的人员,其中大多数人没有内置的事件概念,当然也没有VB的自动装配行为。 So there was no need to introduce this behavior into the language, but instead, new C# developers would just learn the "right way" of doing things from the start. 因此,无需将这种行为引入语言中,而是,新的C#开发人员将从一开始就学习做事的“正确方法”。

With the introduction of WPF and MVVM view/model separation and the push for minimal code-behind, a handles keyword becomes a bit more attractive, but it still seems to go against the general principles of how C# handles events. 随着WPF和MVVM视图/模型分离的引入以及对最小化代码隐藏的推动, handles关键字变得更具吸引力,但它似乎仍然与C#处理事件的一般原理背道而驰。 I suspect it would take a very, very strong argument to convince the C# team that it was worth implementing. 我怀疑要说服C#团队值得实施需要非常非常有力的论据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM