2
votes

In Flex application I have found a way to save target object to "string";

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

Is there a way to read that file and show it as UIComponent in AIR?
This is my try:

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

And I'm getting this error:

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

Any solution would will be fine... as long it works :)

1

1 Answers

-1
votes

You need to register a class alias for BorderContainer before writing to the ByteArray. Otherwise the object's type cannot be preserved and the container is written as an ordinary Object to the ByteArray.

Check the documentation for registerClassAlias() to get detailed information about how to read/write (or with the more common terms: serialize/deserialize) strongly typed objects in ActionScript.

And when I'm right, you need to register the class alias in both applications the Web-based one and AIR.