强制转换在不同应用程序域中加载的对象时出现类型强制错误

发布于 2024-11-04 02:40:19 字数 755 浏览 1 评论 0原文

我的应用程序当前包含许多由小部件管理器管理的小部件。当用户单击某个小部件(例如助手小部件)时,小部件管理器会使用以下代码行将该小部件加载到单独的同级应用程序域中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-11-11 02:40:19

我相信您的问题来自于该类未包含在 swf 中。这是因为,如果不使用 swf 中的类来减小文件大小,则 Flash 不会在这些类中进行编译。为了防止这种情况,您只需在该类中使用所需的帮助器类创建一个变量,如下所示:

private var helper:HelperWidget;

看看是否有帮助。

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:

private var helper:HelperWidget;

See if that helps.

弥枳 2024-11-11 02:40:19

我将重新发布我的“评论”作为真正的答案。我猜测该错误不是基于应用程序域,而是基于您正在编译到模块中的类。当 Flex 编译 SWF 时,它会自动优化 SWF 中未使用的类。您可以通过以下两种方式之一将它们强制返回 SWF:

  1. 使用编译器参数 include-libraries 强制 Flex 编译器将该类添加到您的 SWF 中。
  2. 在应用程序中添加一个假变量,以便 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:

  1. Use the compiler argument include-libraries to force the Flex compiler to add the class to your SWF.
  2. 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;

霞映澄塘 2024-11-11 02:40:19

在尝试了多种解决方案失败后(包括本页上的其他响应),我求助于另一个事件驱动的解决方案。

当 Helper 小部件完成加载时,我最终调度了一个自定义事件来通知我的其他小部件:

(HelperWidget.mxml)

ViewerContainer.addEventListener(AppEvent.WIDGET_OPEN_TAB, widgetOpenTabHandler); //listen for other widgets to open a tab within the Helper Widget
ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_READY_OPEN_TAB)); //notify other widgets that loading is complete

我的其他小部件侦听此事件以触发,并在完成后调度另一个事件 (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)

ViewerContainer.addEventListener(AppEvent.WIDGET_OPEN_TAB, widgetOpenTabHandler); //listen for other widgets to open a tab within the Helper Widget
ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_READY_OPEN_TAB)); //notify other widgets that loading is complete

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文