dha*_*0us 0 apache-flex event-handling actionscript-3
fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);
private function uploadCompleteHandler(event:Event):void {}
Run Code Online (Sandbox Code Playgroud)
以上是在Actionscript中添加事件侦听器的一种方法.默认情况下,回调函数需要具有名称为event的参数并且类型为Event.有没有办法声明这个函数没有任何参数:
private function uploadCompleteHandler():void {}
Run Code Online (Sandbox Code Playgroud)
编辑:可以在mxml中添加没有任何参数的事件处理程序.所以一个学生想知道,为什么不能在动作中做同样的事情呢?
原因是在mxml中你写的东西实际上并不是处理程序,而是在处理程序中执行的内容.如果您有-keep产生-动作标志编译(设置它在Flex Builder中点击鼠标右键,打开项目属性,选择Flex编译器,并添加-keep产生的,动作的附加编译器参数),你可以在看生成的组件源,编译器为该事件创建了一个处理程序,而body由您在mxml中编写的组成.
所以如果你有类似的东西:
click="doSomething();"
Run Code Online (Sandbox Code Playgroud)
你已经注意到你实际上在那里给出了一个指令,这不是你使用addEventHandler时传递的方法引用.
那么你将在生成的文件中有类似的东西:
private function myComponent_Click(evt : MouseEvent) : void
{
doSomething();
}
Run Code Online (Sandbox Code Playgroud)
在同一文件的其他地方添加事件监听器:
this.addEventListener(MouseEvent.CLICK, myComponent_Click);
Run Code Online (Sandbox Code Playgroud)
注意第二个参数不是函数结果,它是一个函数引用,因为缺少表示函数调用的括号,我们的特定函数也不是getter.
您还可以在mxml中指定更多调用,例如:
click="doSomething(); doSomethingElse();"
Run Code Online (Sandbox Code Playgroud)
您甚至可以将event参数传递给您的方法:
click="doSomething(event);"
Run Code Online (Sandbox Code Playgroud)
无论你在mxml事件的值中写什么(不确定它是否正确使用)都将成为actionscript事件生成的处理程序的主体.