在FLEX中重新发送事件 [英] Re-dispatching event in FLEX

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

问题描述

在FLEX应用程序中,我正在尝试重新发送一个自定义事件。即component1执行dispatchEvnet(event),component2注册事件处理程序,处理程序的唯一功能就是dispatch(event)。最后,component3监听从component2发出的事件。我想要做的是类似于重新抛出异常的概念(和类似的原因)。不同的是,在AS3(Flash 10)中重新发送似乎不起作用。在IE中,没有任何事情发生,在FF3中有一个例外说,在调用component3中的处理程序时,尝试强制将事件类型强制为我的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()方法,以使其复制自定义类的属性。如果您未设置在事件子类中添加的所有属性,则当侦听器处理redispatched事件时,这些属性将不具有正确的值。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆