【发布时间】: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。