我无法将自定义事件从一个模块调度到另一个模块,因为它给出了TypeError:错误#1034:类型强制失败:

时间:2011-03-23 06:12:07

标签: flex actionscript-3 events event-dispatching

我正在尝试从一个Flex模块向另一个模块发送自定义事件。

发送事件的代码如下

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);

7 个答案:

答案 0 :(得分:3)

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

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

可以在此处找到对此的一些解释:Module domains

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

dispatchEvent(new Event(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));

答案 1 :(得分:2)

调度时遇到了同样的问题,解决了覆盖两个函数的问题:

override public function clone():Event
{
    return new AlgoEvent(type, bubbles, cancelable);
}

override public function toString():String
{
        return formatToString("AlgoEvent","type"","bubbles","cancelable","eventPhase");
}

希望它有所帮助:)

答案 2 :(得分:1)

先生。 splash提出了一个对我有用的解决方案:

尝试使主应用程序知道Custum事件(我的案例中为Algo事件)类。

即在主应用程序中导入它并创建它的变量..

它起作用的主要原因>>当我们尝试使用事件调度在模块之间进行通信时发生的事情是:模块在运行时加载但类似事件类的类在运行时链接到模块时间.. 但是在加载模块之前编译了Event类。

应用程序在编译时定义一个Custum事件类,模块在发布时定义自己的Custum事件类。然后,当运行应用程序时,应用程序中调度的Custum事件类与模块中的Custum事件类不匹配 瑞士法郎。

对于导致此错误的问题,可以检查链接:

http://www.kirupa.com/forum/showthread.php?t=320390

以及

http://www.jeffdepascale.com/index.php/flash/custom-events-in-loaded-swf-files/

答案 3 :(得分:0)

Mate框架负责所有这些。 它为您的应用程序中的所有模块提供全局事件总线。 http://mate.asfusion.com/

答案 4 :(得分:0)

尝试覆盖自定义事件AlgoEvent中的clone()方法。 将以下代码添加到AlgoEvent.as类并尝试:

  

覆盖公共函数clone():Event {             返回新的AlgoEvent(eventType,bubbles,cancelable);            }   HTH。

答案 5 :(得分:0)

您的自定义Event类应如下所示:

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

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

    override public function clone():AlgoEvent
    {
        return new AlgoEvent(type, bubbles, cancelable);
    };
};

您应该使用Event的继承类型属性,而不是创建新属性。

此外,UIComponent拥有自己的dispatchEvent方法,因此您不必创建自己的方法 - 只有它与继承的方法不同。

此致 罗布

答案 6 :(得分:-1)

好的,必须说从建筑的角度来看,你所做的事情是错误的。调用Application.application很糟糕,特别是如果你开始沿着显示树走下去的话。第二个任何一个孩子都会发生变化,你的构建现在已经破了,直到运行时才会知道它,因为它是一个模块。

您需要的是应用程序框架。一种增加复杂性而不降低可维护性的方法。那里有很多,但我个人最喜欢的是Parsley。我已经在许多非常大的项目中使用它并取得了很大的成功。您现在尝试解决的问题是,调度另一个模块侦听它的一个事件,这非常简单(可以在大约3行代码中完成)。

我建议您查看它和我的presentation on an introduction to parsley