【问题标题】:RaiseEvent Issue while Converting Code C# to VB.NET将代码 C# 转换为 VB.NET 时出现 RaiseEvent 问题
【发布时间】:2015-06-16 22:48:13
【问题描述】:

我有以下 C# 函数,

     private void btnSendtoGeoDecision_Click(object sender, RoutedEventArgs e)
    {
        List<FenceModel> lst = new List<FenceModel>();
        int FencesInsert;

        foreach (FenceModel item in FenceList.SelectedItems)
        {
            lst.Add(item);
        }
        BackgroundWorker worker = new BackgroundWorker();
        //this is where the long running process should go
        worker.DoWork += (o, ea) =>
        {
            //no direct interaction with the UI is allowed from this method

            bool result = objFenceRepository.SendFences(lst, out FencesInsert);
            if (result)
            {
                MessageBox.Show(string.Format("The total of {0} fences have been sent to Geo Decision.", FencesInsert));
            }
            else
            {
                MessageBox.Show(string.Format("The error occurs in sending fences to Geo Decision."));
            }

        };
        worker.RunWorkerCompleted += (o, ea) =>
        {
            //work has completed. you can now interact with the UI
           // _busyIndicator.IsBusy = false;
        };
        //set the IsBusy before you start the thread
      //  _busyIndicator.IsBusy = true;
        worker.RunWorkerAsync();

    }

我使用语言转换器将它转换成 VB.NET,它给了我下面的代码

         Private Sub btnSendtoGeoDecision_Click(sender As Object, e As RoutedEventArgs)
        Dim lst As New List(Of FenceModel)()
        Dim FencesInsert As Integer

        For Each item As FenceModel In FenceList.SelectedItems
            lst.Add(item)
        Next
        Dim worker As New BackgroundWorker()
        'this is where the long running process should go
        worker.DoWork += Function(o, ea)
                             'no direct interaction with the UI is allowed from this method

                             Dim result As Boolean = objFenceRepository.SendFences(lst, FencesInsert)
                             If result Then
                                 MessageBox.Show(String.Format("The total of {0} fences have been sent to Geo Decision.", FencesInsert))
                             Else
                                 MessageBox.Show(String.Format("The error occurs in sending fences to Geo Decision."))

                             End If

                         End Function
        'work has completed. you can now interact with the UI
        ' _busyIndicator.IsBusy = false;
        worker.RunWorkerCompleted += Function(o, ea)

                                     End Function
        'set the IsBusy before you start the thread
        '  _busyIndicator.IsBusy = true;
        worker.RunWorkerAsync()

    End Sub

但是当我构建它时,我得到了两个错误,我知道它要求我使用 RaiseEvent,因为 += 相当于 VB.Net 中的 RaiseEvent,但是有人可以告诉我怎么做吗?

    'Public Event DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. 

    'Public Event RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. 

【问题讨论】:

  • 你试过 Dim WithEvents worker As New BackgroundWorker() 吗?
  • 是的,我收到构建错误“WithEvents 在局部变量声明中无效”
  • 对不起,我忘记了。请参阅我的答案以更正确地使用 WithEvents。

标签: asp.net wpf vb.net events


【解决方案1】:

你需要:

AddHandler worker.DoWork, Sub(o, ea)
                            ' code here 
                          End Function

AddHandler worker.RunWorkerCompleted, Sub(o, ea)
                                       ' code here 
                                      End Function

让您的匿名事件处理程序正常工作。

【讨论】:

  • 幻想我不知道这种语法
  • 是的,它只是 VB.NET 的 lambda 语法!
【解决方案2】:

如果您需要匿名事件处理程序,请关注 Ric 回答。

否则,您必须将 worker 声明为包含您的函数的类/模块的成员变量,例如:

Private WithEvents worker As BackgroundWorker

然后您可以创建命名函数来处理您的事件,如下所示:

Private Sub DoWorkHandler (sender As Object, args As DoWorkEventArgs) Handles worker.DoWork

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多