需求: 需要将某个UI上的几条输入项目存到SharedObject中, 每次Application关闭时进行存储, 开启Application时, 读取上次保存的值并set到ui上.
效果:
每次关闭前, 将会保存最后一次更改的数据; 每次开启前, 将读取上次保存的数据并set到ui上.
代码:
Application Event Listener: creationComplete=”onCreateComplete()” closing=”onClosing(event)”
/** 当Application初始化完毕后响应. */ protected function onCreateComplete(e:Event = null):void { // 在Application启动时, 从SharedObject中读取数据, 刷新UI. var adminConsoleSharedObject:SharedObject = SharedObject.getLocal("adminConsoleSharedObject", "/"); consoleUI.setInitVaules([adminConsoleSharedObject.data.baseUrl, adminConsoleSharedObject.data.userName, adminConsoleSharedObject.data.password]); } /** 当Application关闭时响应, 在每次关闭前, 将有关数据保存到SharedObject中. */ protected function onClosing(event:Event):void { var adminConsoleSharedObject:SharedObject = SharedObject.getLocal("adminConsoleSharedObject", "/"); adminConsoleSharedObject.data.baseUrl = consoleUI.getBaseUrl(); adminConsoleSharedObject.data.userName = consoleUI.getUserName(); adminConsoleSharedObject.data.password = consoleUI.getPassword(); adminConsoleSharedObject.flush(); }
Flex中的ShareObject类似于传统的cookie,但又具有传统cookies所不具有的优点:
1.默认情况下,不会过期
2.默认情况下,每个ShareObject的大小限制为100KB;
3.可以存储简单数据类型 :String,Array,Data等;
4.存储于程序制定的位置[在User的Home Directory中];
5.从不在客户端与服务器间传递
var NoteManagementShareObject:SharedObject = SharedObject.getLocal("NoteManagementShareObjectSave");
var NoteManagementShareObject:SharedObject = SharedObject.getLocal("NoteManagementShareObjectSave", "/");
以保存XML数据为例:
public function save(givenXML:XML = null):void{ var NoteManagementShareObject:SharedObject = SharedObject.getLocal("NoteManagementShareObjectSave"); NoteManagementShareObject.data.catandnote = givenXML; NoteManagementShareObject.flush(); }
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.