本文介绍了在 FLEX 中重新调度事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 FLEX 应用程序中,我试图重新调度"一个自定义事件.IE.组件 1 执行 dispatchEvnet(event),组件 2 为事件注册一个处理程序,处理程序的唯一功能再次是 dispatch(event).最后,component3 侦听来自 component2 的事件.我正在尝试做的类似于重新抛出"异常的概念(出于类似的原因).不同之处在于重新调度在 AS3 (Flash 10) 中似乎不起作用.在 IE 中,什么也没有发生,而在 FF3 中,有一个异常表示在调用 component3 中的处理程序时尝试将 Event 类型强制转换为我的 CustomEvent 时类型转换失败.调试器中的跟踪代码显示,在调用 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(),您将获得 clone() 的基本实现,它返回一个事件.由于 Event 无法转换为您的自定义事件类型,因此会引发运行时错误.

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() 方法,以便它复制您的自定义类的属性.如果您没有设置您在事件子类中添加的所有属性,那么当侦听器处理重新调度的事件时,这些属性将不会具有正确的值.

这篇关于在 FLEX 中重新调度事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:15