2

在 Flex 应用程序中,我找到了一种将目标对象保存到“字符串”的方法;

<?xml version="1.0" encoding="utf-8"?>
<s:Application 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="600">

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        import mx.rpc.xml.SimpleXMLEncoder;
        import mx.utils.ObjectUtil;
        import mx.utils.XMLUtil;

        protected function button1_clickHandler(event:MouseEvent):void
        {

            var fr:FileReference = new FileReference();
            var ba:ByteArray = new ByteArray();
            ba.writeObject(container);
            fr.save(ba);
        }

    ]]>
</fx:Script>
<s:BorderContainer id="container" x="68" y="64" width="341" height="257">
    <s:Label x="69" y="83" width="257" height="124" text="Label"/>
</s:BorderContainer>
<s:Button x="68" y="329" label="SAVE" click="button1_clickHandler(event)"/>

有没有办法读取该文件并将其显示为 AIR 中的 UIComponent?
这是我的尝试:

<?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">

    <fx:Script>
        <![CDATA[
            import flash.events.Event;
            import flash.events.IOErrorEvent;
            import flash.net.FileFilter;
            import flash.net.FileReference;
            import flash.utils.ByteArray;

            import spark.components.BorderContainer;
            private var fr:FileReference;

            private function onLoadFileClick():void
            {
                fr = new FileReference();
                fr.addEventListener(Event.SELECT, onFileSelect);
                fr.addEventListener(Event.CANCEL,onCancel);
                fr.browse();
            }

            private function onFileSelect(e:Event):void
            {
                fr.addEventListener(Event.COMPLETE, onLoadComplete);
                fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
                fr.load();
            }

            private function onCancel(e:Event):void
            {
                trace("File Browse Canceled");
                fr = null;
            }

            private function onLoadComplete(e:Event):void
            {

                var data:ByteArray = fr.data;

                //read the bytes of the file as a string and put it in the
                //textarea


                //outputField.text = data.readUTFBytes(data.bytesAvailable);


                //var obj:BorderContainer = data.;
                cc = data.readObject();

                //clean up the FileReference instance

                fr = null;
            }

            //called if an error occurs while loading the file contents
            private function onLoadError(e:IOErrorEvent):void
            {
                trace("Error loading file : " + e.text);
            }
        ]]>
    </fx:Script>
    <mx:Button label="Load Text File" right="10" bottom="10" click="onLoadFileClick()"/>
    <mx:TextArea right="10" left="10" top="370" bottom="40" id="outputField"/>
    <s:BorderContainer id="cc" x="130" y="99" width="200" height="200">
    </s:BorderContainer>
</s:WindowedApplication>

我收到了这个错误:

TypeError:错误 #1034:类型强制失败:无法转换Object@6f2bc41为[C:\Users\user\Adobe Flash Builder 4.5\primi\src\primi.mxml:51]spark.components.BorderContainerprimi/onLoadComplete()

任何解决方案都会很好......只要它有效:)

4

1 回答 1

-1

BorderContainer在写入 ByteArray 之前,您需要注册一个类别名。否则无法保留对象的类型,并且将容器作为普通对象写入 ByteArray。

查看registerClassAlias() 的文档以获取有关如何在 ActionScript 中读/写(或使用更常见的术语:序列化/反序列化)强类型对象的详细信息。

如果我是对的,您需要在基于 Web 的应用程序和 AIR 应用程序中注册类别名。

于 2012-09-04T00:38:15.767 回答