【问题标题】:Adobe Flex Project: Buttons seem to be calling wrong methodsAdobe Flex 项目:按钮似乎调用了错误的方法
【发布时间】:2015-05-31 00:39:51
【问题描述】:

当我运行此代码时,如果我单击“testExportToCSV”按钮,应用程序将按预期执行 [“将文件保存到...”窗口打开]。但是,一旦我单击该按钮,其他两个按钮就会以某种方式触发同一个窗口 ['save file to...'] 打开,就像我单击了“testExportToCSV”按钮一样。在调试应用程序时,我了解到其他两个按钮确实进入了正确的点击处理方法,尽管最终执行了函数'writeCSV(event:MouseEvent)',该函数在函数'WriteToOut()'中声明,它只是单击“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>

【问题讨论】:

    标签: actionscript-3 flash apache-flex button actionscript


    【解决方案1】:

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

    你可以做到的

    testExportToCSV.addEventListener(MouseEvent.CLICK, writeCSV);

    而且你也必须将上面的语句放在创建完成事件处理程序中,并将函数带到writeOut函数之外。否则它不会第一次执行

    希望对你有帮助

    【讨论】:

    • 非常感谢!这解决了我的问题。如果不是太麻烦,您能解释一下发生了什么(为什么会意外调用那个特定的函数?)。
    • 调用stage.addEventListener(MouseEvent.CLICK, writeCSV);时,整个舞台的任何点击都会触发writeCSV。由于您的其他按钮在舞台上,因此单击事件将触发两个侦听器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多