Flex 和 Cairngorm 错误:C0001E:只能实例化一个 ServiceLocator 实例
我是 Flex 和 Cairngorm 的新手。当我使用 ServiceLocator 时,我确实遇到了问题:错误:C0001E:只能实例化一个 ServiceLocator 实例。
我的代码是这样的:
在 Serives.mxml 中:
<cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*">
<mx:HTTPService id="statistServ"
url="rooms.xml"
showBusyCursor="true"
method="POST"
resultFormat="array"/>
在 Delegate.as 中,我有一些片段:
this.service = ServiceLocator.getInstance().getHTTPService(”statistServ”);
在 Main.xml 中,片段如下:
<business:Service id="service" />
当我加载某个需要 httpservice 的模块的第二个实例时,就会弹出这个美妙的小错误消息。
有没有什么方法可以解决这个问题而不需要切换到另一个框架?
祝愿,
来自中国的硕
I’m new to Flex and Cairngorm.While I’m using ServiceLocator,I do run into the problem: Error: C0001E: Only one ServiceLocator instance can be instantiated.
My Code is like this:
In Serives.mxml:
<cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*">
<mx:HTTPService id="statistServ"
url="rooms.xml"
showBusyCursor="true"
method="POST"
resultFormat="array"/>
In Delegate.as,I have snippets:
this.service = ServiceLocator.getInstance().getHTTPService(”statistServ”);
In Main.xml,snippets like:
<business:Service id="service" />
this wonderful little Error message pops up the minute I load a second instance of some module which requires httpservice.
Is there any way to resolve this problem without switching to another Framework?
Best Wishes,
Shuo from China
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到的错误来自 Cairngorm 的单例模式实现。 它会阻止您创建 ServiceLocator 的第二个实例,因为框架要求只有一个实例。 当加载模块的第二个实例时,您的代码也会尝试创建 ServiceLocator 的第二个实例,而此时它应该使用 getInstance() (或者根本不获取实例)。
通过在 Main.mxml 中声明您的 Services 标记,您已经创建了 ServiceLocator 的唯一实例,并且无法在其他任何地方声明或更新它。 如果 Main.mxml 是您尝试多次实例化的模块的一部分,那么这就是您的问题。 如果没有,我无法根据上面有限的示例判断代码中的问题出在哪里,但它应该在模块中的某个位置。
回复评论:
没问题。 您可以通过在 ActionScript 而不是 MXML 中声明您的服务来轻松解决您的问题。 通过在 MXML 中声明标签,您始终会创建相应 AS 类的实例,而您却想要检索单例的单独实例。 为此,请在模块 MXML 中包含以下 AS 代码:
这基本上等同于您尝试使用 MXML 标记执行的操作,但不创建新的服务实例。
但是,您仍然需要在某处实例化该服务。 为此,请将服务 MXML 标记放入正在加载模块的应用程序的主 MXML 中。
The error you're seeing is from Cairngorm's implementation of the Singleton pattern. It's preventing you from creating a second instance of ServiceLocator because the framework requires that there be only one. When loading the second instance of your module, your code is also trying to create a second instance of ServiceLocator when it should be using getInstance() (or not getting an instance at all).
By declaring your Services tag in Main.mxml, you've created your lone instance of ServiceLocator and it cannot be declared or newed anywhere else. If Main.mxml is part of the module that you're trying to instantiate more than once, that's your problem right there. If not, I can't tell where in your code the problem is based on the limited sample above, but it should be somewhere in the module.
In reply to comment:
No problem. You can easily solve your problem by declaring your Service in ActionScript instead of MXML. By declaring a tag in MXML, you're always creating an instance of the corresponding AS class, whereas you instead want to retrieve the lone instance of the singleton. To do this, include the following AS code inside of your module MXML:
That is essentially equivalent of what you're trying to do with the MXML tag, with the exception of not creating a new Service instance.
However, you still need to instantiate the Service somewhere. To do that, put the Service MXML tag inside of the main MXML of the application which is loading the modules.