将代码C#转换为VB.NET时出现RaiseEvent问题 [英] RaiseEvent Issue while Converting Code C# to VB.NET

查看:460
本文介绍了将代码C#转换为VB.NET时出现RaiseEvent问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下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();

    }

然后我使用Language Converter将其转换为VB.NET,它为我提供了以下代码

And I Convert it into VB.NET Using Language Converter and It gave me below code

         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,但是有人可以告诉我如何做吗?

But when I build it, I get following two Errors, I know its asking me to use RaiseEvent since += equivalent to RaiseEvent in VB.Net, But can Anybody Show me how?

    '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. 

推荐答案

您需要:

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

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

让您的匿名事件处理程序起作用.

For your anonymous eventhandler to work.

这篇关于将代码C#转换为VB.NET时出现RaiseEvent问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆