0

请帮忙

我可以将三个组件中的数据保存为 xml 并且它可以工作,但是现在我正在努力使用代码在用户打开时将该数据读回组件中。这是由用户创建的本地文件。我需要有关打开事件处理程序的帮助。

<?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"
                       width="734" height="389"
                       creationComplete="init();">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->

    </fx:Declarations>


    <fx:Script>
        <![CDATA[

        [Bindable]
        public var xmlData:XML=<ROOTS></ROOTS>;

        private function fnAddItem():void
        {
        var fr:FileReference = new FileReference(); 

        var ba:ByteArray = new ByteArray();
        var newXmlRow:XML=<ROOTS>
        <TXT>{txt1.text}</TXT>
        <TXTA>{txt2.text}</TXTA>
        <DTF>{txt3.text}</DTF>
        </ROOTS>;
        ba.writeMultiByte(newXmlRow, 'utf-8');
        fr.save(ba);
        }


            protected function oped_clickHandler(event:MouseEvent):void
            {
                var fr:FileReference = new FileReference();
                var ba:ByteArray = new ByteArray();
                var newXmlRow:XML=<ROOTS>
                        <TXT>{txt1.text}</TXT>
                        <TXTA>{txt2.text}</TXTA>
                        <DTF>{txt3.text}</DTF>
                        </ROOTS>;
                ba.readMultiByte(xmlData, 'utf-8');
                fr.load(ba);

            }


        ]]>
    </fx:Script>
    <s:Label x="108" y="80" text="Name"/>
    <s:Label x="91" y="222" text="Remarks"/>
    <s:Label x="108" y="116" text="text"/>
    <s:TextInput id="txt1" x="167" y="78"/>
    <s:TextArea id="txt2" x="167" y="218" height="86"/>
    <s:TextArea id="txt3" x="167" y="108" height="77"/>

    <s:Button x="53" y="242" label="save" width="90" click="fnAddItem()"/>
    <s:Button id="oped" x="73" y="271" label="open" click="oped_clickHandler(event)"/>

</s:WindowedApplication>
4

1 回答 1

0

首先,如果你想要一个对话框,你必须等待用户做出选择。选择会引发您可以捕获的事件。在处理程序中,您可以进行文件处理。试试下面的代码是否适合你。

    private var openedFile:File;

    private function oped_clickHandler(event:MouseEvent):void {
        openedFile = new File();
        openedFile.addEventListener(Event.SELECT, file_select);
        openedFile.browseForOpen("Please select a file...");
    }

    private function file_select(event:Event):void {
        if(openedFile != null && openedFile.exists){
            var fileStream:FileStream = new FileStream();
            fileStream.open(openedFile, FileMode.READ);
            var readXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
            fileStream.close();
            trace(readXML.toString());
            txt1.text = readXML.TXT;
            txt2.text = readXML.TXTA;
            txt3.text = readXML.DTF;
        }
        trace(event);
    }

干杯,抢劫

于 2012-09-10T09:05:53.190 回答