在VB中以异步方式使用XMLHTTP对象
XMLHTTP 的异步方式是以回调方式调用Event Handle的,在VB里用:
oXMLHTTP.onreadystatechange = handleFunctionName的方式是不行的。
我曾经想试着用AddressOf 操作符,好像也不行的说。
在MS News 上查到了一个解决方法:
简单的说:把事件的处理函数放到一个VB Class里并把该函数设为Class的Default 方法。
示例如下:
Form1 Code:
--------------------------------------------------------------------------------
Option Explicit
Public oXMLReq As MSXML2.XMLHTTP
Private Sub Command1_Click()
Dim oCls As Class1
Set oXMLReq = New MSXML2.XMLHTTP
oXMLReq.open "POST", "http://localhost/Test/Test.asp", True
Set oCls = New Class1
oXMLReq.onreadystatechange = oCls
oXMLReq.send
End Sub
Class1 Code:
--------------------------------------------------------------------------------
Option Explicit
Public Function Foo()
Debug.Print Form1.oXMLReq.readyState
End Function
记得把Foo设为缺省方法!(在Tools/Procedure Attribute或Class Builder AddIn中均可)
Just Enjoy it. :)
oXMLHTTP.onreadystatechange = handleFunctionName的方式是不行的。
我曾经想试着用AddressOf 操作符,好像也不行的说。
在MS News 上查到了一个解决方法:
简单的说:把事件的处理函数放到一个VB Class里并把该函数设为Class的Default 方法。
示例如下:
Form1 Code:
--------------------------------------------------------------------------------
Option Explicit
Public oXMLReq As MSXML2.XMLHTTP
Private Sub Command1_Click()
Dim oCls As Class1
Set oXMLReq = New MSXML2.XMLHTTP
oXMLReq.open "POST", "http://localhost/Test/Test.asp", True
Set oCls = New Class1
oXMLReq.onreadystatechange = oCls
oXMLReq.send
End Sub
Class1 Code:
--------------------------------------------------------------------------------
Option Explicit
Public Function Foo()
Debug.Print Form1.oXMLReq.readyState
End Function
记得把Foo设为缺省方法!(在Tools/Procedure Attribute或Class Builder AddIn中均可)
Just Enjoy it. :)