Flex 事件侦听器回调未触发

发布于 2024-11-16 02:37:51 字数 463 浏览 2 评论 0原文

也许你可以帮我指出正确的方向。在我们的应用程序中,我定期注意到特定的事件处理程序没有触发。 99% 的时候,它工作得很好,但是,时不时地,它就会死掉。我怎样才能知道发生了什么? DispatchEvent() 是否没有发生/工作?我的听众还在听吗?是否有其他东西捕获了该事件,但没有将其传递以便“正确的”侦听器可以到达它?

这是一些代码...... code

这是实际代码的精简版本,但我认为我没有删除任何重要的内容。我认为关键是我们启动参数对话框,然后开始监听关闭事件。然后,我们展示参数对话框关闭函数。失败时会发生的情况是,永远不会生成跟踪消息“caught close event..”,因此根本不会调用 closeHandler。

我没觉得那里有什么不妥之处,你觉得呢?

那么,我可以使用什么工具来追踪这个小家伙呢?

谢谢!

Maybe you can help point me in the right direction on this. In our app, I am periodically noticing that a particular event handler is not firing. 99% of the time, it works fine, but, every so often, it just dies. How can I find out whats happening? Is the DispatchEvent() not happening/working somehow? Is my listener still listening? Did something else catch the event, and not pass it along so that the 'right' listener can get to it ?

Here's a little bit of the code...
code

Thats a somewhat pruned down version of what the real code is, but I don't think I trimmed out anything important. The key, as I see it is that we fire up the params dialog, then start to listen for the closed event. Then, we show the param dialogs close function. What happens when it fails is that the trace message "caught close event.." is never generated, and, consequently, the closeHandler is not getting called at all.

I don't see anything out of place there, do you?

So, what tools are at my disposal to track this little bugger down?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-11-23 02:37:51

您正在处理从组件内部的显示列表中删除组件的操作。您可以在实际删除对象之前分派 CLOSE 事件,但它很可能在对象已被删除时到达。这是由于事件的异步性质造成的。这意味着,每隔一段时间,当事件到达时,处理事件的函数就不再存在。当然,如果您使用弱引用。

解决方案

现在解决这个问题:

  • 您可以在组件外部处理关闭事件
  • 如果您想将处理程序保留在组件内部, ,您可以在事件处理程序中删除该组件。请记住,在该闭包内,您不能使用 PopUpManager.removePopUp(this),因为“this”不是指该组件,而是指闭包本身。
  • 您可以使用弱引用,但这不是一个好主意,因为您的组件不会因此而被垃圾收集。除非您从闭包中手动删除事件侦听器,

如下所示:

var closeHandler:function = function(e:CloseEvent):void {
    trace("...");
    var p:Params = e.currentTarget as Params;
    p.removeEventListener(CloseEvent.CLOSE, closeHandler);

    /* other code that you want to execute in the closure */
}

var p:Params = PopUpManager.createPopUp(myApp, Params, true) as Params);
p.addEventListener(CloseEvent.CLOSE, closeHandler);
PopUpManager.centerPopUp(p);

You're handling the removal of your component from the displaylist from inside the component. You dispatch the CLOSE event before you actually remove it, but it may very well arrive when the object is already removed. That's due to the asynchronous nature of events. This means that every once in a while the function that handles the event, simply doesn't exist anymore when the event arrives. Certainly if you use a weak reference.

solution

Now to solve the issue:

  • you could handle the close event outside of your component
  • if you want to keep the handler inside the component, you could remove the component in the eventhandler. Keep in mind that inside that closure you can not use PopUpManager.removePopUp(this), since 'this' doesn't refer to the component, but to the closure itself.
  • you could not use a weak reference, but that's not a very good idea, since your component won't be garbagecollected because of that. That is unless you remove the event listener manually from within the closure,

like this:

var closeHandler:function = function(e:CloseEvent):void {
    trace("...");
    var p:Params = e.currentTarget as Params;
    p.removeEventListener(CloseEvent.CLOSE, closeHandler);

    /* other code that you want to execute in the closure */
}

var p:Params = PopUpManager.createPopUp(myApp, Params, true) as Params);
p.addEventListener(CloseEvent.CLOSE, closeHandler);
PopUpManager.centerPopUp(p);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文