Adobe Flex 项目:按钮似乎调用了错误的方法

Adobe Flex Project: Buttons seem to be calling wrong methods

当我 运行 此代码时,如果我单击 'testExportToCSV' 按钮,应用程序将按预期执行 ['save file to...' window 打开]。但是,一旦我单击该按钮,其他两个按钮就会以某种方式触发相同的 window ['save file to...'] 打开,就好像我单击了 'testExportToCSV' 按钮一样。在调试应用程序时,我了解到其他两个按钮确实进入了正确的点击处理方法,尽管以某种方式最终执行了在函数 'WriteToOut()' 中声明的函数 'writeCSV(event:MouseEvent)' ,该函数仅在'testExportToCSV' 按钮被点击。

请解释为什么会这样

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   minWidth="955" minHeight="601"
                   backgroundColor="#3b5998" 
                   creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import mx.rpc.http.HTTPService;

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {}

        /** ########    Click Handlers    ######## */
        protected function csvButton_clickHandler(event:MouseEvent):void{
            trace("...inside csvButton_clickHandler(event:MouseEvent) ");
            textBox.text = "In Export to csv method";
            writeToOut();
        }
        protected function queryServer_clickHandler(event:MouseEvent):void{
            trace("...inside queryServer_clickHandler(event:MouseEvent) ");
            textBox.text = "In query server method";
            serviceRequest.url = "https://msppluto.com/Planholders_Flex.php?customerId=15";
            serviceRequest.send();
        }
        protected function refresh_clickHandler(event:MouseEvent):void{
            trace("...inside refresh_clickHandler(event:MouseEvent) ");
            textBox.text = "In refresh method";
            validateNow();
        }

        /** ########    Write to ...    ######## */
        protected function writeToOut():void{
            var myArray:Array = [];

            for(var i:int = 0; i < 100; i++)
                myArray[i] = {a:Math.random()*100, b:Math.random()*100, c:Math.random()};

            var csv:String = '';

            for(i = 0; i < 100; i++)
                csv += myArray[i].a + ',' + myArray[i].b + ',' + myArray[i].c + '/n';

            stage.doubleClickEnabled = true;
            stage.addEventListener(MouseEvent.CLICK, writeCSV);

            // why is this method being called when I click a different
            // button ?????????????????????????????????????????
            function writeCSV(event:MouseEvent):void{
                var file:FileReference = new FileReference();
                var bytes:ByteArray = new ByteArray();
                bytes.writeUTFBytes(csv);
                file.save(bytes, 'test.csv');
            }
        } // end writeToOut

    ]]>
</fx:Script>

<fx:Declarations>
    <s:HTTPService
        id="serviceRequest"
        url="https://msppluto.com/Planholders_Flex.php"
        useProxy="false">
    </s:HTTPService>
</fx:Declarations>

<s:Label id="headline" text="HTTP Service" fontSize="45" x="10" y="10" color="#FFFFFF"></s:Label>
<s:Button id="testExportToCSV" x="14" y="187" minWidth="110" label="Export to csv" click="csvButton_clickHandler(event)"/>
<s:Button id="queryServerButton" x="14" y="216" minWidth="110" label="Query Server" click="queryServer_clickHandler(event)"/>
<s:Button id="refreshScreen" x="14" y="245" minWidth="110" label="refresh" click="refresh_clickHandler(event)"/> 
<s:TextInput id="textBox" x="197" y="216" widthInChars="25"  />

<mx:DataGrid id="dataGridForServiceRequest" x="575" y="160"
    dataProvider="{serviceRequest.lastResult.planHolders.planHolder}">
    <mx:columns>
        <mx:DataGridColumn headerText="User ID" dataField="userid"/>
        <mx:DataGridColumn headerText="User Name" dataField="username"/>
    </mx:columns>
</mx:DataGrid>

我不知道问题的背景是什么。但问题出在线路上 stage.addEventListener(MouseEvent.CLICK, writeCSV);

你能做到

testExportToCSV.addEventListener(MouseEvent.CLICK, writeCSV);

而且你也必须将上面的语句放在creation complete event handler中,并且将函数放在writeOut函数之外,否则不会第一次执行

希望对您有所帮助