将“Handles”从VB.NET迁移到C#

前端之家收集整理的这篇文章主要介绍了将“Handles”从VB.NET迁移到C#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在将一些代码从VB.NET迁移到C#(3.5).

我找到了以下结构:

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

将此类行为转换为C#的最直接方法是什么?

我建议使用 Telerik Code Converter作为开始.

C#没有像VB.NET那样通过“Handles”关键字轻松自动附加事件处理程序.

//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);
    }
}

此外,您需要将事件处理程序分配给对象,如下所示:

Button1.Click += Button1_Click;

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

但是C#确实具有这样做的简洁能力:

Button1.Click += (sender,e)=>
{
    //do something
}
原文链接:https://www.f2er.com/vb/255525.html

猜你在找的VB相关文章