Flex 自定义事件调度 [英] Flex Custom Event dispatching

查看:22
本文介绍了Flex 自定义事件调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 flex 事件调度的问题.

I got a question about event dispatching in flex.

我的目标是让自定义事件加载一些数据,而不是冒泡到事件监听器.

my goal is to get a custom event loaded up with some data and than bubble up to the eventlistener.

我的主应用程序有一个 AMF 服务请求,其中调用了一个服务类.当 AMF 服务请求返回结果或错误并且主应用程序正在侦听该事件时,该类应该调度一个事件.

my main application has and AMF service request inside which calls an service class. that class is supposed to dispatch an event when the AMF service request returns a result or fault and the main application is listening for that event.

所以在我的 mainapp 中,我添加了这样的监听器:

so inside my mainapp i add and listener like this:

this.addEventListener("UserInfoEvent", userInfoHandler);

自定义 UserInfoEvent.as 如下所示

the custom UserInfoEvent.as looks like this

package events
{
 import flash.events.Event;

 import logic.Ego;

 public class UserInfoEvent extends Event
 {
  private var ego:Ego;

  public function UserInfoEvent(type:String, ego:Ego)
  {
   super(type);
   this.ego = ego;
  }

  override public function clone():Event 
  { 
   return new UserInfoEvent(type, ego);
  } 
 }
}

最后,AMF 服务类中的调度程序如下所示:

and finally the dispatcher inside the AMF Service class looks like this:

private var dispatch:EventDispatcher = new EventDispatcher;  
var userInfoEvent:UserInfoEvent = new UserInfoEvent("UserInfoEvent", ego);
dispatch.dispatchEvent(userInfoEvent);

但是,此事件永远不会到达主应用程序.用调试器仔细观察它,我发现当事件被调度时,它的 listeners 属性被设置为 null.

However, this event never reaches the main application. Looking closely to it with the debugger, i found out that when the event is dispatched the listeners property of it is set to null.

谁能告诉我我做错了什么?

Can anyone tell me what i'm doing wrong?

推荐答案

在我看来,您没有以正确的方式收听事件.

You are not listening to the event in the right way as I see it.

 // you have 
 this.addEventListener("UserInfoEvent", userInfoHandler);

 // but you dispatch the event from    
 dispatch.dispatchEvent(userInfoEvent);  

哪里不对

你需要有类似的东西

instance_of_Dispatch.addEventListener();  // because this != dispatch

这篇关于Flex 自定义事件调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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