apache-flex - 我无法将自定义事件从一个模块分派(dispatch)到另一个模块,因为它给出了 TypeError : Error #1034: Type Coercion failed:

标签 apache-flex actionscript-3 events event-dispatching

我正在尝试将自定义事件从一个 flex 模块分派(dispatch)到另一个。

发送事件的代码如下

Application.application.Destination.child.dispatchEvent(
    new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));

这里AlgoEvent是自定义事件

另一方面,捕获和处理事件的模块具有以下代码:

public  function sendParametersToChild(e:AlgoEvent):void
{
    //some codes
}

但是当执行语句 Application.application.Destination.child.dispatchEvent(new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT)); 时,调试器给出以下运行时异常:

TypeError: Error #1034: Type Coercion failed: cannot convert resources.events::AlgoEvent@4182239 to resources.events.AlgoEvent.
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298]
    at components::Destination/sendTimeToChild()[E:\FlexProjects\MyApp\src\components\Destination.mxml:99]
    at components::Destination/updateParameters()[E:\FlexProjects\MyApp\src\components\Destination.mxml:206]
    at components::Destination/__CreateBasketButton_click()[E:\FlexProjects\MyApp\src\components\Destination.mxml:558]

我无法确定这里出了什么问题。 请帮忙解决这个问题

这是我的事件类

public class AlgoEvent extends Event
{
    public static const GETFROMPARENT_LOCAL_EVENT:String = "getfromparent_local";
    private var eventType:String;

    public function AlgoEvent(eventType:String, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(eventType,bubbles,cancelable);
        this.eventType=eventType;
    }
}

调试时在 UIComponent 类的这个函数中出现错误

override public function dispatchEvent(event:Event):Boolean
{
    if (dispatchEventHook != null)
        dispatchEventHook(event, this);

    return super.dispatchEvent(event); 
}

正是这一行给出了错误:dispatchEventHook(event, this);

最佳答案

在主应用程序中导入 AlgoEvent创建对它的引用。

import resources.events.AlgoEvent;
private var dummyEvent: AlgoEvent;

一些解释可以在这里找到:Module domains

如果您的自定义事件不带有任何特殊的事件属性,您可以使用标准的 Event 类解决您的问题。

dispatchEvent(new Event(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));

关于apache-flex - 我无法将自定义事件从一个模块分派(dispatch)到另一个模块,因为它给出了 TypeError : Error #1034: Type Coercion failed:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5401472/

相关文章:

MySQL选择事件前7天和事件后7天

apache-flex - 如何激活 Flex 应用程序,以便用户不必在其中单击即可开始获取键盘事件

java - 在 ActionScript3 中创建对象相等 "HashMap"作为 java HashMap

apache-flex - Flex 数据网格中的行顺序

actionscript-3 - 弹性 AS3 : Find and remove elements from container of certain class

events - AngularJS 广播不适用于第一个 Controller 负载

actionscript-3 - 垃圾收集到底是什么?在 ActionScript 3.0 中你是如何做到的?

flash - 如何在 as3 中弯曲文本?

apache-flex - 有没有办法覆盖 ActionScript 运算符,特别是我想覆盖等号运算符

javascript - 渲染完成时的 Echarts 事件