我正在尝试将自定义事件从一个 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/