WPF后台工作者无法识别工人对象

时间:2009-03-11 17:06:07

标签: wpf vb.net backgroundworker

我有以下代码将后台工作程序添加到VB.net WPF项目中:

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.BackgroundWorker
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.ServiceProcess
Partial Public Class Window1
Public Sub New()
    MyBase.New()
    Me.InitializeComponent()
    End Sub
End Class
Public Class Window1
Dim worker As New BackgroundWorker
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.BackgroundWorker) Handles worker.DoWork

   End Sub
End Class

我收到DoWork工作人员事件的以下错误:

  

Handles子句需要在包含类型或其基类型之一中定义WithEvents变量。

似乎它在事件声明中缺少某些内容,但无法找到它。

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

在声明新的backgroundworker时尝试添加'WithEvents'。下面是来自Windows窗体设计器生成代码的一个后台工程对象的代码片段:

Friend WithEvents bWorker As System.ComponentModel.BackgroundWorker

请告诉我这是否有帮助!

答案 1 :(得分:3)

您的DoWork事件的签名看起来很时髦 - 不应该是(Object,DoWorkEventArgs)。

你有(Object,BackgroundWorker)

答案 2 :(得分:2)

尝试替换

Dim worker As New BackgroundWorker

Private WithEvents worker As New BackgroundWorker