简体   繁体   English

将asp.net团队从VB.Net迁移到C#,似乎我们在连接事件方面缺少什么?

[英]Moving asp.net team from VB.Net to C#, seems are we are missing something with wiring up events?

An asp.net (mostly webforms) team I work on has recently decided to make the move from vb.net to c#. 我正在工作的asp.net(主要是Webforms)团队最近决定从vb.net转到c#。 The people who used to be on the team who couldn't read/write C# code well had since moved on, and each time we hired a new person it became more of a problem for us to be a vb.net shop. 此后,曾经不能很好地读写C#代码的团队中的人开始迁移,每次我们雇用一个新人时,成为vb.net商店就成为一个问题。

I'll save the standard vb/c# language war stuff and just stick with the one thing that I ran into today. 我将保存标准的vb / c#语言战争资料,并坚持使用我今天遇到的一件事。

With vb.net, when we want to handle some event from some object, we just pick the object out of the list in the upper left, pick the event out of the list on the upper right, and it creates the fully wired up event handler. 使用vb.net,当我们要处理某个对象的某些事件时,我们只需从左上​​角的列表中选择该对象,从右上角的列表中选择该事件,然后创建完全连线的事件处理程序。 It's also very clear that the method is an event handler due to the "Handles ... " at the end. 同样非常清楚的是,由于最后使用了“ Handles ...”,因此该方法是一个事件处理程序。

In the C# world, it seems we are left with a few poor options. 在C#世界中, 似乎我们还有一些糟糕的选择。

1) Manually wireup the event by typing the name of the event and event handler in the aspx markup. 1)通过在aspx标记中键入事件的名称和事件处理程序来手动连接事件。 Like actually writing "OnLoad=SomeFunction" in the aspx. 就像在aspx中实际编写“ OnLoad = SomeFunction”一样。 (EDIT: I didn't realize that his only applied to the form and not to any contained controls, so I guess this isn't really an option) (编辑:我没有意识到他仅适用于表单,而不适用于任何包含的控件,因此我想这并不是一个选择)

2) Using auto wireup events, and naming methods and signatures correctly so that they match up. 2)使用自动连线事件,并正确命名方法和签名,以使其匹配。

I'm hoping I'm missing some other much better option. 我希望我缺少其他更好的选择。

With these 2 options that I've seen so far: 到目前为止,我已经看到了以下两个选项:

1) we have to know the event names and event arguments. 1)我们必须知道事件名称和事件参数。 We can't just look down the list and be like "Oh good this has OnItemClick", I have to go research that in some control documentation. 我们不能只看列表,而要像“哦,这有OnItemClick”,我必须去研究一些控制文档。 Likewise I'd have to go research the event args to find out that ItemClick needs a RadPanelBarEventArgs as the 2nd parameter. 同样,我必须去研究事件args才能发现ItemClick需要RadPanelBarEventArgs作为第二个参数。 Wasted time. 浪费时间。

2) I get no compile check on anything I'm doing with regard to connecting events. 2)我没有任何关于连接事件的编译检查。 If I name the method signature wrong, or if I type it in wrong in the aspx page, everything will compile just fine until runtime when the mismatch will blow things up. 如果我将方法签名命名为错误,或者如果在aspx页面中键入错误的方法签名,则一切都会正常编译,直到运行时出现不匹配的情况为止。 (EDIT: what I mean is if I type onClick="ABC" and the event handler is really named "ABCD" this will not be caught at compile time. With VB, issues with renaming/mistyping event handler names are caught at compile time, because of the "Handles". Same goes for if I have onClick="ABC" and this controls onClick really needs a SpecialEventArgs but I accidentally use SomeRegularEventArgs) (编辑:我的意思是,如果我键入onClick =“ ABC”且事件处理程序的名称为“ ABCD”,则在编译时将不会捕获该事件。使用VB,在编译时会捕获到重命名/错误键入事件处理程序名称的问题,因为存在“句柄”。如果我有onClick =“ ABC”,并且此控件onClick确实需要SpecialEventArgs,但我不小心使用SomeRegularEventArgs,也是如此。

I'm hoping we missed something and there is a better way to do this. 我希望我们错过了一些东西,并且有更好的方法可以做到这一点。

This is a known 'bug' or missing feature. 这是已知的“错误”或缺少的功能。 See Visual studio 2010 showing available events from code behind 请参阅Visual Studio 2010,其中显示了来自代码背后的可用事件

If you want to auto-generate events in C#: 如果要在C#中自动生成事件:

  • Go to your aspx page in Design View. 在设计视图中转到您的aspx页面。
  • Make sure the properties panel is visible 确保属性面板可见
  • Either select a control with your mouse, or use the dropdown under properties to select the control. 用鼠标选择一个控件,或使用属性下的下拉列表选择控件。
  • Move to the Events tab (the lightning bolt) 移至“事件”选项卡(闪电)
  • Find the event you want to wire up and either write in a name and hit Enter, leave it empty and hit Enter, or Double-click it. 找到您要关联的事件,然后输入名称并按Enter,将其保留为空,然后按Enter,或双击它。
  • It will create the event handler with all the properties. 它将创建具有所有属性的事件处理程序。

As for Number 2. Are you putting your code in a code-behind file? 至于2号。您是否将代码放在代码隐藏文件中? .aspx.cs? .aspx.cs?

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

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