在 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)
我不是专家,但以下内容对我有用。 (为保护无辜者,化名)。
这是我的自定义事件之一:
import flash.events.Event;
public class SomethingHappenedEvent extends Event {
public static const SOMETHING_COMPLETE:String = "PictureTakenComplete";
public var passedValue:int;
public function SomethingHappenedEvent(i:int) {
passedValue = i;
super(SOMETHING_COMPLETE);
}
}
我设置了一个侦听器来侦听我的自定义事件。当自定义事件触发时,我的 doSomething() 函数将被执行。以下是我的设置方法:
mySecret.addEventListener(SomethingHappenedEvent.SOMETHING_COMPLETE,doSomething);
在 doSomething()
函数内,我可以访问事件的 passedValue
参数:
private function doSomething(e:SomethingHappenedEvent):void {
trace("did something: " + e.passedValue);
}
最后,以下是我在代码中其他位置分派自定义事件的方法,传入一个参数:
this.dispatchEvent(new SomethingHappenedEvent(myCounter));
- 使用数据参数创建自定义事件
- 您的类((例如 MyObject)应该使用 addEventListener 捕获 Event.COMPLETE 并使用其中的数据分派您的自定义事件 MyCustomEvent 在
- 其他地方捕获您的 MyCustomEvent(例如在 MyAnotherObject 中)
MyCustomEvent.as
package
{
import flash.events.Event;
public class MyCustomEvent extends Event
{
public static const MY_CUSTOM_EVENT_COMPLETE:String = "myCustomEventComplete";
public var data:Object;
// Constructor
public function CaretEvent(type:String, data:Object, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, true, cancelable);
this.data = data;
}
// Public Methods
public override function clone():MyCustomEvent
{
return new MyCustomEvent(type, data, bubbles, cancelable);
}
}
}
MyObject.as
package
{
import flash.events.EventDispatcher;
import flash.events.Event;
public class MyObject extends EventDispatcher
{
// Contructor
public function MyObject()
{
addEventListener(Event.COMPLETE, onComplete)
}
// Private Methods
private function onComplete(event:Event):void
{
dispatchEvent(new MyCustomEvent(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, "This is my custom data"));
}
}
}
MyAnotherObject 。作为
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
public class MyAnotherObject extends EventDispatcher
{
// Constructor
public function MyAnotherObject()
{
addEventListener(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, onMyCustomEventComplete)
}
// Private Methods
private function onMyCustomEventComplete(event:MyCustomEvent):void
{
trace(event.data);
}
}
}
从事情的声音来看,如果您需要的只是另一个属性,您可能不需要扩展 FileReference、Event.COMPLETE 等......以下内容是否适合您的目的?如果没有,请向我展示一些代码,以便我们可以在同一页面上。
public function go():void {
var downloadURL:URLRequest = new URLRequest("http://example.com/big.jpg");
var otherParam:String = "another param";
var fileRef:FileReference = new FileReference();
fileRef.addEventListener(Event.COMPLETE, function(e:Event):void {
handleLoad(e,otherParam); });
fileRef.download(downloadURL);
}
public function handleLoad(e:Event,whatelse:String):void {
Alert.show("we just loaded " + e.target + " with param " + whatelse);
}
这将在完成这项工作的 handleLoad()
函数中获取一个额外的参数 (whatelse
)。或者你是说在下载时你没有附加参数?
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
首先,您需要照常向
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: