在FLEX重新调度事件事件、FLEX

2023-09-09 21:23:24 作者:心累

在Flex应用程序,我想再派遣自定义事件。即COMPONENT1确实dispatchEvnet(事件),COMPONENT2注册一个事件的处理,该处理程序被再次讯(事件)的唯一功能。最后,component3侦听出来COMPONENT2的事件。我所试图做的是类似于再抛异常的概念(以及类似的原因)。不同的是,重新调度似乎没有在AS3工作(闪光灯10)。在IE中,没有任何反应,而在FF3有一个例外,说,试图胁迫事件类型,以我的自定义事件,同时调用component3处理程序的类型转换失败。追踪code在调试器中显示的时候component3被调用时,该事件确实是一个通用的,用我所有的自定义的东西丢失了。难道这是这种情况?

In a FLEX app, I am trying to "re-dispatch" a custom event. I.e. component1 does dispatchEvnet(event), component2 registers a handler for the event, the only function of the handler being, again, dispatch(event). Finally, component3 listens for the event coming out of component2. What I am trying to do is similar to the concept of "re-throwing" exceptions (and for similar reasons). The difference is that re-dispatching does not seem to work in AS3 (Flash 10). In IE, nothing happens, and in FF3 there is an exception saying that the type cast failed while trying to coerce the Event type to my CustomEvent while calling the handler in component3. Tracing code in the debugger shows that by the time component3 is called, the event is, indeed, a generic one, with all my custom stuff lost. Is this supposed to be the case?

推荐答案

您遇到是由于未覆盖的的clone()在你的自定义事件的事件。

The problem you are experiencing is caused by not overriding the clone() event in your custom event.

当事件被重新调度,他们被克隆和修改。如果不重写的clone()你得到的基实现克隆(),它返回一个事件。由于事件不能转换到您的自定义事件类型,运行时错误被抛出。

When events are redispatched, they are cloned and modified. If you don't override clone() you get the base implementation of clone(), which returns an Event. Because Event cannot be cast to your custom event type, a runtime error is thrown.

从文档:

在创建自己的自定义Event类时,必须覆盖继承的Event.clone()方法,以便它复制自定义类的属性。如果您没有设置所有您在事件子类中添加的所有属性,这些属性将不会有正确的值当侦听器处理重新调度的事件。

When creating your own custom Event class, you must override the inherited Event.clone() method in order for it to duplicate the properties of your custom class. If you do not set all the properties that you add in your event subclass, those properties will not have the correct values when listeners handle the redispatched event.