2012-09-04 18 views
2

在Flex應用程序中,我找到了一種將目標對象保存爲「字符串」的方法;用戶界面組件從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)"/> 

有沒有辦法讀取該文件並顯示爲UIComponent的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> 

而且我得到這個錯誤:

TypeError: Error #1034: Type Coercion failed: cannot convert [email protected] to spark.components.BorderContainer at primi/onLoadComplete() [C:\Users\user\Adobe Flash Builder 4.5\primi\src\primi.mxml:51]

任何解決辦法都會好起來的......只要它的工作原理:)

回答

-1

您需要在寫入ByteArray之前爲BorderContainer註冊類別別名。否則,對象的類型不能被保留,容器被寫爲ByteArray的普通對象。

檢查documentation for registerClassAlias()以獲取有關如何在ActionScript中讀取/寫入(或使用更常見的術語:serialize/deserialize)強類型對象的詳細信息。

而當我是對的,你需要在這兩個應用程序中註冊類別別名和AIR。