Flex - 在收到标准事件后调度自定义事件

发布于 2024-07-14 11:50:01 字数 740 浏览 6 评论 0原文

我正在处理用户在以下代码中选择的多个文件的 FileReferenceList.fileList[] 。

for (i=0;i < event.currentTarget.fileList.length; i ++){
 fileByteData = new ByteArray();
 fileByteData = (event.currentTarget.fileList[i].data as ByteArray);                
 loader = new Loader();
 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, checkImageDimensions);
 loader.loadBytes(fileByteData);                    
}

我需要将 i 传递到 checkImageDimensions 来跟踪哪个图像是哪个,我可以轻松地创建一个自定义事件,但我需要这个在正确的时间开火。 理想情况下我可以做这样的事情..

var myEvent:CustomEvent = new CustomEvent(i);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.dispatchEvent(CustomEvent))

但说实话,我不确定如何继续...

任何人都可以帮助我吗? 谢谢!

I am processing a FileReferenceList.fileList[] of multiple files a user selects in the following code..

for (i=0;i < event.currentTarget.fileList.length; i ++){
 fileByteData = new ByteArray();
 fileByteData = (event.currentTarget.fileList[i].data as ByteArray);                
 loader = new Loader();
 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, checkImageDimensions);
 loader.loadBytes(fileByteData);                    
}

I need to pass i into checkImageDimensions to keep track of which image is which, I can easily enough create a custom event, but I need this to fire at the right time. Ideally I could do something like this..

var myEvent:CustomEvent = new CustomEvent(i);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.dispatchEvent(CustomEvent))

But to be honest, I am unsure of how to proceed...

Can anyone help me out? Thanks!

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

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

发布评论

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

评论(1

勿忘心安 2024-07-21 11:50:02

您需要创建一个侦听器函数 - 函数调用是不够的。 将类名传递给 dispatchEvent 也不起作用。 以下是具体操作方法。

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
    dispatchEvent(new CustomEvent(i));
});

请注意,如果事件中所需的只是字符串类型,则不一定需要自定义事件类。 在这种情况下,您可以简单地使用 Event 类:

public static const MY_CUSTOM_EVENT:String = "myCustomEvent";    
...
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
    dispatchEvent(new Event(MY_CUSTOM_EVENT));
});

Edit:如果您真的很懒,但仍然想通过事件传递值,则可以使用 DinamicEvent 类:

var evt:DynamicEvent = new DynamicEvent(MY_CUSTOM_EVENT);
evt.attr1 = val1;
evt.attr2 = val2;
dispatchEvent(evt);

当然,声明自己的错误类更干净且不易出错。

You need to make a listener function - a function call does not suffice. Passing a class name to dispatchEvent does not work either. Here's how to do it.

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
    dispatchEvent(new CustomEvent(i));
});

Note that you don't necessarily need a custom event class, if all you need in the event is a type string. You can simply use the Event class in this case:

public static const MY_CUSTOM_EVENT:String = "myCustomEvent";    
...
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
    dispatchEvent(new Event(MY_CUSTOM_EVENT));
});

Edit: If you're really lazy, but still want to pass values with the event, you can use the DinamicEvent class:

var evt:DynamicEvent = new DynamicEvent(MY_CUSTOM_EVENT);
evt.attr1 = val1;
evt.attr2 = val2;
dispatchEvent(evt);

Of course, it is cleaner and less error prone to declare your own error class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文