Lotus Symphony 1.2
|
建立一個接聽程式實例。
許多 Uno 介面允許您在特定的接聽程式介面上註冊接聽程式,這樣就可以接聽特定的事件,並可以呼叫適當的接聽程式方法。CreateUnoListener 函數等待呼叫的接聽程式介面,然後將一個此介面支援的物件傳送給介面。然後此物件被傳送到註冊接聽程式的方法中。
以下範例以 Basic 程式庫物件為基礎。
Dim oListener
oListener = CreateUnoListener ( "ContListener_","com.sun.star.container.XContainerListener" )
CreateUnoListener 方法需要兩個參數。第一個參數是首碼 (prefix),下面進行詳細介紹。第二個參數是要用的接聽程式介面之完整名稱。
須將接聽程式加入廣播物件。呼叫適當的接聽程式加入程序以完成此動作。這些程序一定遵循 addFooListener 規則,其中 Foo 是沒有 X 的接聽程式介面類型。在此範例中,呼叫 addContainerListener 程序以註冊 XContainerListener:
Dim oLib
oLib = BasicLibraries.Library1 ' Library1 必須存在!
oLib.addContainerListener ( oListener ) ' 註冊接聽程式
接聽程式現已註冊。事件發生時,相應的接聽程式將從 com.sun.star.container.XContainerListener 介面呼叫適當程序。
首碼從 Basic 子常式中呼叫註冊接聽程式。Basic 執行時期系統搜尋名為 PrefixListenerMethode 的 Basic 子常式或函數,並在找到時呼叫之。否則,將發生執行時期錯誤。
在此範例中,接聽程式介面使用以下程序:
disposing:
接聽程式基本介面 (com.sun.star.lang.XEventListener):所有接聽程式介面的基本介面
elementInserted:
com.sun.star.container.XContainerListener 介面的程序
elementRemoved:
com.sun.star.container.XContainerListener 介面的程序
elementReplaced:
com.sun.star.container.XContainerListener 介面的程序
在此範例中,字首是 ContListener_。因此,需要在 Basic 中執行下列子常式:
ContListener_disposing
ContListener_elementInserted
ContListener_elementRemoved
ContListener_elementReplaced
每個接聽程式類型中都有一個包含事件資訊的事件結構類型。呼叫接聽程式程序時,事件實例會當作一參數,傳送到該程序。只要 Sub 宣告中傳送了相應參數,Basic 接聽程式程序就會呼叫這些事件物件。例如:
Sub ContListener_disposing ( oEvent )
MsgBox "disposing"
MsgBox oEvent.Dbg_Properties
End Sub
Sub ContListener_elementInserted ( oEvent )
MsgBox "elementInserted"
MsgBox oEvent.Dbg_Properties
End Sub
Sub ContListener_elementRemoved ( oEvent )
MsgBox "elementRemoved"
MsgBox oEvent.Dbg_Properties
End Sub
Sub ContListener_elementReplaced ( oEvent )
MsgBox "elementReplaced"
MsgBox oEvent.Dbg_Properties
End Sub
如果不使用事件物件,不需含括事件物件的參數:
' Sub disposing 的最低執行
Sub ContListener_disposing
End Sub
警告:必須一律實作接聽程式方法,以避免發生 Basic 執行時期錯誤。