在FLEX文档中所述的是,如果你要向监听器传送参数,那么你最好使用MXML标签,但是在这里要展示给你的是如何使用AS脚本完成。
使用内联函数
solution is very simple, basically for following scenario :
arbitrary button called ABCButton,function ABCButtonListener needs to listen to click event and also receive extra parameter
"myPrivateSpecialObject"object.
private function ABCButtonListener(e:MouseEvent,specialObj:Object): void {P.S -----------------
Alert.show(specialObj.name);
}
private function methodWhereyouDostuffAndRegisterListener(): void {
var myPrivateSpecialObject:Object = {name:"Special String Ingredients for Orange"};
ABCButton.addEventListener(MouseEvent.CLICK,function (e:MouseEvent) : void {
ABCButtonListener(e,myPrivateSpecialObject);
});
}
private function methodWhereyouDostuffAndRegisterListener(): void {
var myPrivateSpecialObject:Object = {name:"Special String Ingredients for Orange"};
ABCButton.addEventListener(MouseEvent.CLICK,function (e:MouseEvent) : void {
ABCButtonListener(e,myPrivateSpecialObject);
});
myPrivateSpecialObject = {name:"Special Ingredients for Apple"};
OtherButtonButton.addEventListener(MouseEvent.CLICK,function (e:MouseEvent) : void {
ABCButtonListener(e,myPrivateSpecialObject);
});
}
myPrivateSpecialObjectso, reference is changed to have different value, but if you assume now each listener received different value , its not correct, they will both received latest assigned value . overall inline function is good technique to pass any number of parameters to a listener as long as listener is ready for those parameters.