在 Flex/AS3 中将属性附加到事件
我有一个需要与事件一起传递的参数。在尝试通过扩展类将其放置在类型上失败后,我在另一个 SO 问题中被建议编写一个自定义事件。
我之前尝试过,但不知道如何发送它,因为我只想在 FileReference 上使用正常的 Event.COMPLETE
事件发送另一个参数。我不能让 Event.COMPLETE
触发,然后分派我的事件,因为那样我就会在同一条船上......所需的参数不会在初始 Event.COMPLETE 中
对象,因此我将无法调度一个新事件,将该参数传递给实际执行工作的代码。
非常感谢所有对此的帮助,非常需要它。谢谢。
基本上,我需要完全按原样替换 Event.COMPLETE
功能,我只需要一个能够保存我的额外属性的事件。请告诉我有什么办法可以做到这一点。
I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I've been advised in another SO question to write a custom event.
I tried this before but I couldn't figure out how to send it, because I just want to send another parameter with a normal Event.COMPLETE
event on a FileReference. I can't have Event.COMPLETE
fire and then dispatch my event because then I'll be in the same boat ... the needed parameter won't be in the initial Event.COMPLETE
object so I won't be able to dispatch a new event passing that parameter on to the code that actually performs the work.
All help on this is greatly appreciated, need it badly. Thanks.
Basically, I need to replace the Event.COMPLETE
functionality exactly as it is, I just need an event that will hold my extra property. Please tell me there's some way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,您需要照常向
Event.COMPLETE
添加一个事件侦听器,因为它将自动分派,您对此无能为力。在该事件处理程序中,您可以调度您自己的自定义,即使应用程序的其余部分使用附加的自定义属性进行侦听。这是一个简单的示例:您的事件处理代码如下所示:
MyEvent
类如下所示:您现在可以像任何其他事件一样为自定义事件添加事件侦听器:
first you need to add an event listener to the
Event.COMPLETE
as normal since it is going to be dispatched automatically and theres nothing you can do about it. Inside that event handler you can then dispatch your own custom even that the rest of the application listens for with your custom property attached to it. heres a quick example:your event handling code would look like:
and the
MyEvent
class looks like:you can now add event listeners for your custom event just like any other event:
我不是专家,但以下内容对我有用。 (为保护无辜者,化名)。
这是我的自定义事件之一:
我设置了一个侦听器来侦听我的自定义事件。当自定义事件触发时,我的 doSomething() 函数将被执行。以下是我的设置方法:
在
doSomething()
函数内,我可以访问事件的passedValue
参数:最后,以下是我在代码中其他位置分派自定义事件的方法,传入一个参数:
I'm not an expert, but the following is working for me. (Names changed to protect the innocent).
Here is one of my custom events:
I have a listener set up to listen for my custom event. When the custom event fires, my
doSomething()
function is executed. Here's how I set that up:Inside the
doSomething()
function I have access to event'spassedValue
parameter:Finally, here is how I dispatch a custom event elsewhere in the code, passing in a parameter:
MyCustomEvent.as
MyObject.as
MyAnotherObject 。作为
MyCustomEvent.as
MyObject.as
MyAnotherObject.as
您能否发布一些代码,以便我们了解您是如何解决这个问题的?
不过,我强烈建议您阅读 flex live 文档 关于创建和使用自定义事件。
这里是一个简单的自定义基于事件的应用程序,看看源代码,它可能就是您正在寻找的。
Could you post some code so we can see how you are approaching this?
However I would highly recommend reading the flex live docs on creating and using custom events.
Here is a simple custom event based application, Take a look at the source, it might be what you are looking for.
我通常会做这样的事情,也许不是最好的方法,但你应该知道这种方法是可用的......
I usually do something like this, maybe not the best approach, but you should know this way is available...
从事情的声音来看,如果您需要的只是另一个属性,您可能不需要扩展 FileReference、Event.COMPLETE 等......以下内容是否适合您的目的?如果没有,请向我展示一些代码,以便我们可以在同一页面上。
这将在完成这项工作的
handleLoad()
函数中获取一个额外的参数 (whatelse
)。或者你是说在下载时你没有附加参数?From the sound of things, you may not need to extend FileReference, Event.COMPLETE, etc. if all you need is another property... will the following work for your purposes? If not, show me some code so we can be on the same page.
That will get an extra parameter (
whatelse
) into thehandleLoad()
function that does the work. Or are you saying that at the time of the download you don't have the additional parameter?