我创建了一个带有 2 个按钮的组件。每个按钮的单击功能都绑定到一个变量。我需要将组件中定义的默认按钮处理程序替换为应用程序中定义的处理程序。
缩放按钮.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
preinitialize="onPreinitialize(event)">
<mx:script>
import mx.events.FlexEvent;
[Bindable] public var zoomOutHandler:Function;
[Bindable] public var zoomInHandler:Function;
protected function onPreinitialize(event:FlexEvent):void {
zoomOutHandler = localZoomOutHandler;
zoomInHandler = localZoomInHandler;
}
private function localZoomOutHandler(event:MouseEvent):void {
// Do nothing.
}
private function localZoomInHandler(event:MouseEvent):void {
// Do nothing.
}
</mx:script>
<mx:Button id="zoomOut" label="-" width="20"
toolTip="Zoom Out" click="{zoomOutHandler}" />
<mx:Button id="zoomIn" label="+" width="20"
toolTip="Zoom In" click="{zoomInHandler}" />
</mx:HBox>
应用程序.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
mx:local="*"
creationComplete="onCompelete(event)">
<mx:script>
[Binable] public var scale:Number = 1.0;
protected function onComplete(event:FlexEvent):void {
zoomButtons.zoomOutHandler = handleChangeZoomOut;
zoomButtons.zoomInHandler = handleChangeZoomIn;
}
private function handleChangeZoomOut(event:MouseEvent):void {
scale /= 2;
}
private function handleChangeZoomIn(event:MouseEvent):void {
scale *= 2;
}
</mx:script>
<local:ZoomButtons id="zoomButtons" />
</mx:Application>