Actionscript 3 UI组件从Flex到Air

Actionscript 3 UI组件从Flex到Air,actionscript-3,apache-flex,air,Actionscript 3,Apache Flex,Air,在Flex应用程序中,我找到了一种将目标对象保存为“字符串”的方法 有没有办法读取该文件并在空中显示为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/sp

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


有没有办法读取该文件并在空中显示为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
to
spark.components.BorderContainer
at
primi/onload complete()
[C:\Users\user\Adobe Flash Builder 4.5\primi\src\primi.mxml:51]


任何解决方案都可以。。。只要有效:)

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

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


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

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

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

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

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