Difference between revisions of "Zh/Documentation/DevGuide/ProUNO/Basic/Instantiating UNO Services"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
m
 
Line 7: Line 7:
 
|NextPage=Zh/Documentation/DevGuide/ProUNO/Basic/Getting Information about UNO Objects
 
|NextPage=Zh/Documentation/DevGuide/ProUNO/Basic/Getting Information about UNO Objects
 
}}
 
}}
[[en:Documentation/DevGuide/ProUNO/Basic/Instantiating UNO Services]]
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/ProUNO/Basic/{{SUBPAGENAME}}}}
 
{{DISPLAYTITLE:实例化 UNO 服务}}
 
{{DISPLAYTITLE:实例化 UNO 服务}}
  

Latest revision as of 02:52, 14 May 2009



在 Basic 中,使用 Basic 运行时库 (RTL) 函数 createUnoService() 实例化服务。该函数需要一个全限定的服务名称,如果提供这样一个名称,函数将返回一个支持该服务的对象:

 oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )


此调用实例化 com.sun.star.ucb.SimpleFileAccess 服务。为确保函数成功,可以使用 IsNull 函数检查返回的对象:

 oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )
 bError = IsNull( oSimpleFileAccess )' bError is set to False
 
 oNoService = CreateUnoService( "com.sun.star.nowhere.ThisServiceDoesNotExist" )
 bError = IsNull( oNoService )' bError is set to True


除了使用 CreateUnoService() 实例化服务以外,也可以通过调用 GetProcessServiceManager() 来获取 OpenOffice.org 进程的全局 UNO com.sun.star.lang.ServiceManager。获取后,可以直接使用 createInstance()

 oServiceMgr = GetProcessServiceManager()
 oSimpleFileAccess = oServiceMgr.createInstance( "com.sun.star.ucb.SimpleFileAccess" )
 
 ' is the same as
 
 oSimpleFileAccess = CreateUnoService( "com.sun.star.ucb.SimpleFileAccess" )


GetProcessServiceManager() 的优势是:使用服务管理器实例化服务时,可以接收附加信息和传入的参数。例如,要初始化带有参数的服务,就必须在服务管理器中使用 com.sun.star.lang.XMultiServiceFactorycreateInstanceWithArguments() 方法,因为没有相应的 Basic RTL 函数可执行此操作。示例:

 Dim args(1)
 args(0) = "Important information"
 args(1) = "Even more important information"
 oService = oServiceMgr.createInstanceWithArguments _
     ( "com.sun.star.nowhere.ServiceThatNeedsInitialization", args() )


GetProcessServiceManager() 返回的对象是一个支持 com.sun.star.lang.ServiceManager 的一般 Basic UNO 对象。可以按照上面所述那样访问其属性和方法。


此外,Basic RTL 提供若干作为 API 入口点的特殊属性。OpenOffice.org Basic 和对话框 - OpenOffice.org Basic 功能 中将对它们进行详细介绍:

OpenOffice.org Basic RTL 属性 说明
ThisComponent 仅存在于 Writer、Calc、Draw 或 Impress 文档嵌入的 Basic 代码中。它包含嵌入 Basic 代码的文档模型。
StarDesktop 办公软件应用程序的 com.sun.star.frame.Desktop singleton。它装入文档组件并处理文档视窗。例如,可以使用 oDoc = StarDesktop.CurrentComponent
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages