强制转换在不同应用程序域中加载的对象时出现类型强制错误
我的应用程序当前包含许多由小部件管理器管理的小部件。当用户单击某个小部件(例如助手小部件)时,小部件管理器会使用以下代码行将该小部件加载到单独的同级应用程序域中:
wgtInfo.load(null, null, null, moduleFactory); //wgtInfo = IModuleInfo
但是,我以后无法使用该小部件的变量和函数。我尝试从小部件管理器的小部件列表中找到帮助程序小部件,并且成功了。但是,当我尝试将 Helper Widget 从 IBaseWidget 类型(所有小部件共享的接口)转换为 HelperWidget 类型时,我收到以下错误:
TypeError:错误#1034:类型强制失败......
这是因为尝试使用 Helper Widget 的类的应用程序域与 Helper Widget 的应用程序域不同。我尝试通过将所有小部件加载到与加载程序相同的应用程序域中来解决此问题:
wgtInfo.load(ApplicationDomain.currentDomain, null, null, moduleFactory);
现在,每当我尝试加载 Helper 小部件时,都会收到以下错误:
TypeError:错误#1009:无法访问空对象引用的属性或方法。
如何将我的 Helper 小部件加载到其他小部件可访问的公共应用程序域中?
My application currently contains a number of Widgets that are managed by a Widget Manager. When the user clicks on a widget (e.g. a Helper widget), the Widget Manager loads the widget into a separate sibling application domain with the following line of code:
wgtInfo.load(null, null, null, moduleFactory); //wgtInfo = IModuleInfo
However, I am unable to use the widget's variables and functions later on. I attempt to find the Helper widget from the Widget Manager's list of widgets, and I do successfully. But when I try to caste the Helper Widget from type IBaseWidget (the interface all widgets share) to type HelperWidget, I receive the following error:
TypeError: Error #1034: Type Coercion failed.....
This is because the application domain of the class trying to use the Helper widget is different from the application domain of the Helper Widget. I tried to fix this by loading all widgets into the same application domain as the loader:
wgtInfo.load(ApplicationDomain.currentDomain, null, null, moduleFactory);
I now get the following error whenever I attempt to load the Helper widget:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
How can I load my Helper widget into a common application domain that is accessible by the other widgets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您的问题来自于该类未包含在 swf 中。这是因为,如果不使用 swf 中的类来减小文件大小,则 Flash 不会在这些类中进行编译。为了防止这种情况,您只需在该类中使用所需的帮助器类创建一个变量,如下所示:
看看是否有帮助。
I believe your problem comes from the class not being included in the swf. This is because Flash doesn't compile in classes in a swf if they aren't used to reduce filesize. To prevent this, you only need to create a variable with the helper class you need in that class, like this:
See if that helps.
我将重新发布我的“评论”作为真正的答案。我猜测该错误不是基于应用程序域,而是基于您正在编译到模块中的类。当 Flex 编译 SWF 时,它会自动优化 SWF 中未使用的类。您可以通过以下两种方式之一将它们强制返回 SWF:
在应用程序中添加一个假变量,以便 Flex 编译器认为它已被使用并将其添加到最终的 SWF 中。像这样的东西。
private var myFakeObject : HelperWidth;
I'm gonna repost my 'comment' as a real answer. I'm guessing that the error is not based on the ApplicationDomain but based on which classes you are being compiled into your Module. When Flex compiles the SWF it automatically optimizes unused classes out of the SWF. You can force them back into the SWF in one of two ways:
Add a fake variable in in your application so that the Flex compiler thinks it is used and adds it to the final SWF. Something like this.
private var myFakeObject : HelperWidth;
在尝试了多种解决方案失败后(包括本页上的其他响应),我求助于另一个事件驱动的解决方案。
当 Helper 小部件完成加载时,我最终调度了一个自定义事件来通知我的其他小部件:
(HelperWidget.mxml)
我的其他小部件侦听此事件以触发,并在完成后调度另一个事件 (AppEvent.WIDGET_OPEN_TAB) 来执行Helper 小部件中的功能。
After trying a number of solutions unsuccessfully (including the other responses on this page), I resorted to another, event-driven, solution.
I ended up dispatching a custom event to notify my other widgets when the Helper widget had completed loading:
(HelperWidget.mxml)
My other widgets listen for this event to fire, and upon completion, dispatch another event (AppEvent.WIDGET_OPEN_TAB) to perform a function within the Helper widget.