简体   繁体   English

VB到C#-事件处理程序的自动命名与显式接线

[英]VB to C# - auto naming of event handlers vs explicit wiring

Sorry if this is a duplicate; 很抱歉,如果这是重复的; I have searched extensively for a basic explanation. 我已经广泛搜索了基本解释。

TL;DR - I already know how to wire up events, my question relates to supporting information and event handler naming convention(s) , which I cannot find an answer to. TL; DR-我已经知道如何连接事件,我的问题与支持信息和事件处理程序命名约定有关 ,我找不到答案。 Having already read here , here , here , here , and here I think information overload has confused me, or I'm overlooking the obvious. 我已经在这里这里这里这里这里阅读 ,我认为信息过载使我感到困惑,或者我忽略了显而易见的信息。

I have a huge web forms (VB) solution that needs converting to C#, starting with some Web Forms Server Control projects. 我有一个庞大的Web窗体(VB)解决方案,需要从某些Web Forms Server Control项目开始转换为C#。 Within each, VB uses the Handles word to wire up event handlers: 在每个内部,VB使用Handles单词来连接事件处理程序:

Public Class Accordion
    Inherits CompositeControl

    Private Sub Accordion_Init(sender As Object, e As System.EventArgs) Handles Me.Init

    End Sub

    Private Sub AccordionPanelHolder_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub AccordionPanelHolder_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

    End Sub

End Class

When using any kind of code converter, and from my research, the C# approach means manually wiring the event handlers (code shortened for brevity): 使用任何类型的代码转换器时,根据我的研究,C#方法意味着手动连接事件处理程序(为简洁起见,代码已缩短):

public Accordion()
{
    PreRender += AccordionPanelHolder_PreRender;
    Load += AccordionPanelHolder_Load;
    Init += Accordion_Init;
}
private void AccordionPanelHolder_PreRender(object sender, System.EventArgs e)
{
    // ...
}

I'm happy with the above, however, if I create a new web form in VS then the default code is simply this: 我对上述内容感到满意,但是,如果我在VS中创建新的Web表单,则默认代码就是这样:

public partial class TestPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // to do
    }
}

There is no event logic handler logic in the page. 该页面中没有事件逻辑处理程序逻辑。 I see that AutoEventWireUp="True" , when usually I would have it as False . 我看到AutoEventWireUp="True" ,通常我将其设为False My questions therefore: 因此,我的问题是:

  1. What/Where is the naming convention defined that C# uses to auto wire up event handlers, as per the last example? 根据最后一个示例,C#用于自动绑定事件处理程序的命名约定定义在什么位置/在哪里? I'm nervous that methods could run twice. 我担心方法可能会运行两次。 I see that VS doesn't have the drop-down functionality for C# to create empty event handlers are that wired up. 我看到VS没有C#的下拉功能来创建空事件处理程序。
  2. If AutoEventWireUp="False" , do I have to manually wire up every single event (I expect so, which is a good thing [I think...])? 如果AutoEventWireUp="False" ,我是否必须手动连接每个事件(我希望如此,这是一件好事[我认为...])?

When AutoEventWireUp is true, the following methods would be looked for: AutoEventWireUp为true时,将寻找以下方法:

  • Page_PreInit Page_PreInit
  • Page_Init Page_Init
  • Page_InitComplete Page_InitComplete
  • Page_PreLoad Page_PreLoad
  • Page_Load 的Page_Load
  • Page_LoadComplete Page_LoadComplete
  • Page_DataBind Page_DataBind
  • Page_SaveStateComplete Page_SaveStateComplete
  • Page_PreRender Page_PreRender
  • Page_PreRenderComplete Page_PreRenderComplete
  • Page_Unload 激发Page_Unload
  • Page_Error Page_Error事件
  • Page_AbortTransaction Page_AbortTransaction
  • Page_CommitTransaction Page_CommitTransaction

So, the methods must use this exact name. 因此,方法必须使用这个确切的名称。

If you set AutoEventWireUp to false, you'll have to add the handler for any of this events by hand. 如果将AutoEventWireUp设置为false,则必须手动添加任何此类事件的处理程序。

You can have a look here for more info. 您可以在这里查看更多信息。

Not entirely sure if this is a correct answer, but you should think of it as wire up the event from when it is needed. 不能完全确定这是否是正确的答案,但您应该将其视为从需要的时候开始关联事件。 If this is from the creation of your class you can add the hooks in the constructor, or as in asp.net the Page_Load delegate. 如果这是从创建类开始的,则可以在构造函数中或在asp.net中的Page_Load委托中添加钩子。

Something like this 像这样

void Page_Load(object sender, EventArgs e)
{
    PreRender += AccordionPanelHolder_PreRender;
    Load += AccordionPanelHolder_Load;
    Init += Accordion_Init;
}

But it was a long time ago I was working with Web Forms, so maybe there's something I'm forgetting, but this is similar to how I would do it in a regular C# application. 但是很久以前,我正在使用Web Forms,所以也许有些事情我忘记了,但这与我在常规C#应用程序中的处理方式类似。

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

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