本文介绍了将“句柄”从VB.NET迁移到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些代码从VB.NET迁移到C#(3.5)。



我发现如下结构:



pre> 公共事件DataLoaded(ByVal sender As Object,ByVal e As EventArgs)

受保护的子Mag_Button_Load_Click(ByVal sender As Object,ByVal e As EventArgs)句柄Mag_Button_Load.C​​lick
[..]
RaiseEvent DataLoaded(Me,EventArgs.Empty)
End Sub
[..]

'其他类
Private Sub LoadData(ByVal sender As Object,ByVal e As System.EventArgs)处理oData.DataLoaded
[..]
End Sub

将这种行为转换为C#最简单的方法是什么?

解决方案

我建议您先使用作为开始。



C#没有通过像VB.NET这样的Handles关键字自动附加事件处理程序。

 code> // EV entHandler声明
public event EventHandler DataLoaded;
protected void Mag_Button_Load_Click(object sender,EventArgs e)
{

//提升事件
如果(DataLoaded!= null){
DataLoaded ,EventArgs.Empty);
}
}

此外,您需要将事件处理程序分配给这样的对象:

  Button1.Click + = Button1_Click; 

protected void Button1_Click(object sender,EventArgs e)
{
// do something。
}

然而,C#确实有这样做的简洁能力:

  Button1.Click + =(sender,e)=> 
{
//做某事
}


I'm migrating some code from VB.NET to C# (3.5).

I find structures like:

Public Event DataLoaded(ByVal sender As Object, ByVal e As EventArgs)

Protected Sub Mag_Button_Load_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Mag_Button_Load.Click
[..]
        RaiseEvent DataLoaded(Me, EventArgs.Empty)
End Sub
[..]

'Other Class
Private Sub LoadData(ByVal sender As Object, ByVal e As System.EventArgs) Handles oData.DataLoaded
[..]
End Sub

What is the most straightforward way to translate such behaviour to C#?

解决方案

I recommend using the Telerik Code Converter as a start.

C# does not have that easy automatic attaching of event handlers by means of the "Handles" keyword like VB.NET does.

//EventHandler declaration
public event EventHandler  DataLoaded;
protected void Mag_Button_Load_Click(object sender, EventArgs e)
{

    //Raise Event
    if (DataLoaded != null) {
        DataLoaded(this, EventArgs.Empty);
    }
}

Also, You need to assign your event handlers to the objects like this:

Button1.Click += Button1_Click;

protected void Button1_Click(object sender, EventArgs e)
{
  //do something.
}

However C# does have the succinct ability of doing this as well:

Button1.Click += (sender, e)=>
{
    //do something
}

这篇关于将“句柄”从VB.NET迁移到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:12