简体   繁体   English

Flex序列化用于内置组件,例如……HBox,VBox,Panel,Canvas,DataGrid

[英]Flex Serialization for inbuilt components like…HBox, VBox, Panel, Canvas, DataGrid

Here is the problem...I'm working on a flex application(actionscript)... 这是问题...我正在开发flex应用程序(actionscript)...

I have a Panel in my application which contains 2 buttons and 3 canvas components at certain posstions...now I want to store the current state of panel in some file or database...and afterwards I want to load the same panel again in my application when I come back and run the application... 我的应用程序中有一个面板,在某些位置包含2个按钮和3个画布组件...现在我想将面板的当前状态存储在某个文件或数据库中...然后,我想再次在其中加载相同的面板当我返回并运行该应用程序时...

so I tried to convert whole panel into ByteArray object using its readObject() and writeObject() methods...but when I read the ByteArray and add the panel in my application using addChild() method it doesn't add anything and there was no error or fault... 因此我尝试使用其readObject()和writeObject()方法将整个面板转换为ByteArray对象...但是当我读取ByteArray并使用addChild()方法在应用程序中添加面板时,它不会添加任何内容,并且没有错误或错误...

writeObject creates ByteArray that I am able to print but when i get it back and add child, I am not able to get the panel and it's children... writeObject创建了我可以打印的ByteArray,但是当我取回它并添加子对象时,我将无法获取面板及其子对象...

if anyone can help...it would be appreciated...thanks in advance... 如果有人可以帮助...将不胜感激...提前感谢...

Here is the example code...explaining what i want to do... 这是示例代码...解释我要做什么...

<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;
        import mx.collections.ArrayCollection;

    [Bindable] private var photoFeed:ArrayCollection;

    var buffer:ByteArray;

    private function init():void{

        addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
        searchTerms.setFocus();
        buffer = new ByteArray();
    }

    private function keyHandler(event:KeyboardEvent):void{
        if(event.charCode == 13){

                    myButton.label = "Exit";

            try{
                buffer.writeObject(myData);

            }catch(error:Error){
                Alert.show(error.toString(),"Encoding Error");
            }

            removeChild(myData);
            reloadButton.visible = true;
            Alert.show("HBox is deleted","Alert");
        }
    }

    private function reloadHBox():void{

        Alert.show("Trying to load Hbox","Alert"); 
        try{
            buffer.position = 0;
            var obj:HBox = buffer.readObject() as HBox;

        }catch(error:Error){
            Alert.show(error.toString(),"Decoding Error");
        }

        addChild(obj);

        Alert.show("Hbox is reloaded","Alert"); 
    }

    ]]>
</mx:Script>

<mx:Button id="reloadButton" label="Reload HBox" visible="false" click="reloadHBox()"/>

<mx:HBox width="100%" id="myData">
    <mx:Label text="Hi Rashmin here..."/>
    <mx:TextInput id="searchTerms" name="searchTerms" text="Hello How are you?"/>
    <mx:Button id="myButton" label="Enter"/>
</mx:HBox>

I want to regenerate the HBox so need some help... 我想重新生成HBox,所以需要一些帮助...

Creative idea, but I'm not surprised it doesn't work. 有创意,但我并不惊讶它不起作用。 Can you share some code? 可以共享一些代码吗?

That said, I'd just write up an algorithm to save the state (x, y coordinates / height width etc... ) and reset that info when you load it. 也就是说,我只是编写了一种算法来保存状态(x,y坐标/高度宽度等),并在加载时重置该信息。

You could create an object that stores your panel position 您可以创建一个存储面板位置的对象

 private var positions:Object;
 positions = { panel1Position: new Point( panel1X , panel1Y)
                          //etc.... };

Set some default values to start with and your components would get their positions from your positions object. 设置一些默认值开始,您的组件将从您的positions对象获取位置。

  private function init():void
  {
     panel1.x = positions.panel1Position.x;
     //etc...
  }

To save your values, use a SharedObject 要保存您的值,请使用SharedObject

 var objectName:String = "Put some identifier here";
 var sharedObject:SharedObject = SharedObject.getLocal( objectName , '/' );
 sharedObject.data.positions = positions;

To retrieve your values, you just need 要检索您的值,您只需要

 var sharedObject = SharedObject.getLocal( "the identifier you've set above" , '/' );
 positions = sharedObject.data.positions;

You can then update your components x & y values. 然后可以更新组件的x和y值。 Anyway, this is the general idea, for more info check the SharedObject class: 无论如何,这是一个总体思路,有关更多信息,请检查SharedObject类:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/ http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/

You can apply the same principle with your panel states, if you can identify each state with an integer for instance, save the state integer in your sharedObject. 您可以对面板状态应用相同的原理,例如,如果可以用整数标识每个状态,则将状态整数保存在sharedObject中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM