Flex使用没有参数的回调函数添加事件侦听器

时间:2021-09-12 21:22:25
fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);

  private function uploadCompleteHandler(event:Event):void {}

Above is one way to add an event listener in Actionscript. By default the callback function needs to have an argument with name event and type Event. Is there a way to declare this function without any arguments :

以上是在Actionscript中添加事件侦听器的一种方法。默认情况下,回调函数需要具有名称为event的参数并且类型为Event。有没有办法声明这个函数没有任何参数:

  private function uploadCompleteHandler():void {}

Edit : It's possible to add an event handler without any arguments in mxml. So one student wanted to know, why isn't it possible to do the same in actionscript?

编辑:可以在mxml中添加没有任何参数的事件处理程序。所以一个学生想知道,为什么不能在动作中做同样的事情呢?

2 个解决方案

#1


The reason is that in mxml what you write isn't actually the handler, is what gets executed in the handler. If you compile with -keep-generated-actionscript flag (To set it in Flex Builder right click to open the project Properties, select Flex Compiler, and add -keep-generated-actionscript to the Additional compiler arguments), you can see in the generated source for your component, that the compiler created a handler for that event, and the body is composed by that you wrote in mxml.

原因是在mxml中你所写的实际上并不是处理程序,而是在处理程序中执行的内容。如果使用-keep-generated-actionscript标志进行编译(要在Flex Builder中右键单击以打开项目属性,选择Flex Compiler,并将-keep-generated-actionscript添加到Additional编译参数),您可以在生成的组件源,编译器为该事件创建了一个处理程序,而body由您在mxml中编写的组成。

So if you have something like:

所以,如果你有类似的东西:

click="doSomething();"

You can already notice that you're actually giving an instruction there, that's not a method reference you're passing like when you use addEventHandler.

您已经注意到您实际上在那里发出了一条指令,这不是您使用addEventHandler时传递的方法参考。

Then you'll have in the generated file something like:

然后你将在生成的文件中有类似的东西:

private function myComponent_Click(evt : MouseEvent) : void
{
    doSomething();
}

And somewhere else in the same file the adding of the event listener:

在同一文件的其他地方添加事件监听器:

this.addEventListener(MouseEvent.CLICK, myComponent_Click);

Notice the second parameter is not a function result, it's a function reference, because the parenthesizes that denote a function call are missing, and our particular function is not a getter either.

注意第二个参数不是函数结果,它是一个函数引用,因为缺少表示函数调用的括号,我们的特定函数也不是getter。

You can also specify more calls in mxml, like:

您还可以在mxml中指定更多调用,例如:

click="doSomething(); doSomethingElse();"

You can even pass the event parameter to your method:

您甚至可以将event参数传递给您的方法:

click="doSomething(event);"

Whatever you write in the value of the mxml event (not sure it's the right term to use) will become the body of the generated handler for the actionscript event.

无论你在mxml事件的值中写什么(不确定它是正确的术语)都将成为actionscript事件生成的处理程序的主体。

#2


No, you'll get a runtime exception when the event fires if you'll try to register it as event listener.

不,如果您尝试将其注册为事件侦听器,则会在事件触发时获得运行时异常。

You may use the following syntax to allow calling your handler directly without event object:

您可以使用以下语法来允许在没有事件对象的情况下直接调用处理程序:

private function uploadCompleteHandler(event:Event = null):void {}

#1


The reason is that in mxml what you write isn't actually the handler, is what gets executed in the handler. If you compile with -keep-generated-actionscript flag (To set it in Flex Builder right click to open the project Properties, select Flex Compiler, and add -keep-generated-actionscript to the Additional compiler arguments), you can see in the generated source for your component, that the compiler created a handler for that event, and the body is composed by that you wrote in mxml.

原因是在mxml中你所写的实际上并不是处理程序,而是在处理程序中执行的内容。如果使用-keep-generated-actionscript标志进行编译(要在Flex Builder中右键单击以打开项目属性,选择Flex Compiler,并将-keep-generated-actionscript添加到Additional编译参数),您可以在生成的组件源,编译器为该事件创建了一个处理程序,而body由您在mxml中编写的组成。

So if you have something like:

所以,如果你有类似的东西:

click="doSomething();"

You can already notice that you're actually giving an instruction there, that's not a method reference you're passing like when you use addEventHandler.

您已经注意到您实际上在那里发出了一条指令,这不是您使用addEventHandler时传递的方法参考。

Then you'll have in the generated file something like:

然后你将在生成的文件中有类似的东西:

private function myComponent_Click(evt : MouseEvent) : void
{
    doSomething();
}

And somewhere else in the same file the adding of the event listener:

在同一文件的其他地方添加事件监听器:

this.addEventListener(MouseEvent.CLICK, myComponent_Click);

Notice the second parameter is not a function result, it's a function reference, because the parenthesizes that denote a function call are missing, and our particular function is not a getter either.

注意第二个参数不是函数结果,它是一个函数引用,因为缺少表示函数调用的括号,我们的特定函数也不是getter。

You can also specify more calls in mxml, like:

您还可以在mxml中指定更多调用,例如:

click="doSomething(); doSomethingElse();"

You can even pass the event parameter to your method:

您甚至可以将event参数传递给您的方法:

click="doSomething(event);"

Whatever you write in the value of the mxml event (not sure it's the right term to use) will become the body of the generated handler for the actionscript event.

无论你在mxml事件的值中写什么(不确定它是正确的术语)都将成为actionscript事件生成的处理程序的主体。

#2


No, you'll get a runtime exception when the event fires if you'll try to register it as event listener.

不,如果您尝试将其注册为事件侦听器,则会在事件触发时获得运行时异常。

You may use the following syntax to allow calling your handler directly without event object:

您可以使用以下语法来允许在没有事件对象的情况下直接调用处理程序:

private function uploadCompleteHandler(event:Event = null):void {}