我有以下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.
答案 0 :(得分:2)
你需要:
AddHandler worker.DoWork, Sub(o, ea)
' code here
End Function
AddHandler worker.RunWorkerCompleted, Sub(o, ea)
' code here
End Function
让你的匿名事件处理程序工作。
答案 1 :(得分:0)
如果您需要匿名事件处理程序,请按照Ric回答。
否则,您必须将worker声明为包含您的函数的类/模块的成员变量,如:
Private WithEvents worker As BackgroundWorker
然后你可以创建像这样处理事件的命名函数:
Private Sub DoWorkHandler (sender As Object, args As DoWorkEventArgs) Handles worker.DoWork