ShareObject

来源:互联网 发布:谷歌翻墙软件 编辑:程序博客网 时间:2024/05/16 17:14

对于ShareObject类的认识主要在于其创建和读取。

ShareObject类的功能类似于cookie,用于保存客户端信息,但是它比cookie更为强大,因为它可以保存更为复杂的数据结构。必须注意的是它不能够保存方法或者函数。

它保存在客户端的文件后缀名为.sol,以我本机上面为例子,看看它保存的路径:C:/Documents and Settings/huang/Application Data/Macromedia/Flash Player/#SharedObjects/AZNKB3RR/localhost/eclipseworkspace/test/bin-debug/ObjectHandles.swf

打开我的user.sol可以看到 7TCSO      userMag   username mnkn password knkn 。这样说明了保存的信息是被加密的了。

创建共享对象。ShareObject类使用静态方法getLocal()获取共享对象,倘若不存在,则新对象会被创建。写入的方法是flush()。这个方法有个参数叫做minimumDiskSpace,用于指定文件大小,默认是100kb。

共享对象的读取。shareObject对象将内容保存在data属性中。与cookie一样,在使用它之前,最好先测试一下引用的内容是否存在。

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" height="300" creationComplete="init()">
 <mx:Script>
  <![CDATA[
         [Bindable] 
   private var so:SharedObject;
   
   private function init():void{
    so=SharedObject.getLocal("userMag");
    if(so.data.username!=undefined){
     this.username.text=so.data.username;
     return;
    }
    if(so.data.password!=undefined){
     this.password.text=so.data.password;
     return;
    }
    
   }
   private function keepMagHandler(event:Event):void{
    if(CheckBox(event.target).selected)
     flushMag();
    else
     removeMag();
   }
   private function flushMag():void{
    trace('seleced');
    so.data.username=this.username.text;
    so.data.password=this.password.text;
    
    so.flush();
   }
   private function removeMag():void{
    trace('not seleced');
    so.clear();
   }
  ]]>
 </mx:Script>
 <mx:Form x="10" y="10" width="360" height="240">
  <mx:FormItem label="username:">
   <mx:TextInput id="username"/>
  </mx:FormItem>
  <mx:FormItem label="password:">
   <mx:TextInput id="password"/>
  </mx:FormItem>
  <mx:FormItem>
   <mx:Button label="submit"/>
  </mx:FormItem>
  <mx:FormItem>
   <mx:CheckBox id="keepMag" click="keepMagHandler(event)" selected="{so.data.username!=null}" label="保留信息"/>
  </mx:FormItem>
 </mx:Form>
 
</mx:TitleWindow>