Flex SharedObject 使用举例

Categories: Flex; Tagged with: ; @ January 14th, 2010 21:07

需求: 需要将某个UI上的几条输入项目存到SharedObject中, 每次Application关闭时进行存储, 开启Application时, 读取上次保存的值并set到ui上.

效果:

image

每次关闭前, 将会保存最后一次更改的数据; 每次开启前, 将读取上次保存的数据并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();
		}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.