Flex4中使用SharedObject共享对象

SharedObject就相当于网页浏览器中的Cookie,用法很简单,这里以字符串类型的对象做简单的示例

<?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" creationComplete="application1_creationCompleteHandler(event)">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>

	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			private var so:SharedObject;

			// 读取SharedObject的值
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				so = SharedObject.getLocal("test1");
				var txt:String;
				if(so.data.label1 == undefined)
				{
					txt = "";
				}
				else
				{
					txt = String(so.data.label1);
				}
				label1.text = txt;
			}
			
			// 设置SharedObject的值
			protected function button1_clickHandler(event:MouseEvent):void
			{
				so.data.label1 = text1.text;
				label1.text = text1.text;
			}

			// 清除SharedObject的值
			protected function button2_clickHandler(event:MouseEvent):void
			{
				so.clear();
				label1.text = "";
			}

		]]>
	</fx:Script>

	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:Label id="label1" x="283" y="84" fontSize="30"/>
	<s:TextInput id="text1" x="134" y="140"/>
	<s:Button x="304" y="141" label="设置" click="button1_clickHandler(event)"/>
	<s:Button x="410" y="141" label="清除" click="button2_clickHandler(event)"/>
	<s:Label x="134" y="84" text="对象的值:" fontSize="30"/>
	
</s:Application>
posted @ 2010-12-04 11:11  魔豆  阅读(1751)  评论(0编辑  收藏  举报