【永恒的瞬間】
          ?Give me hapy ?
          Flash 中提供了下面的操作本地對象的方法:
          SharedObject.clear()
          刪除本地共享對象
          SharedObject.flush()
          立即把共享對象數(shù)據(jù)寫入本地文件
          SharedObject.getLocal()
          創(chuàng)建或連接本地共享對象
          SharedObject.getSize()
          取得本地共享對象的指定大小數(shù)據(jù)
          Flex
          中使用本地共享對象的方法本質(zhì)上和在 Flash 中是相同。

          基本的使用方法如下:

          1. //定義本地共享對象 ?

          2. var ? myLSO : SharedObject ; ?

          3. //創(chuàng)建本地共享對象 ?

          4. myLSO ? = ? SharedObject . getLocal ( 'foo' );

          5. //給共享對象賦值

          6. //可以保存的有數(shù)字、字符串、布爾型、XML、日期型、數(shù)組型和對象等數(shù)據(jù)類型

          7. currentUserName ? = ? 'Liu21st' ;

          8. itemsArray ? = ? new ? Array( 101 , 346 , 483 )

          9. currentUserIsAdmin ? = ? true ;

          10.? myLSO . data . userName ? = ? currentUserName ;

          11.? myLSO . data . itemNumbers ? = ? itemsArray ;

          12.? myLSO . data . adminPrivileges ? = ? currentUserIsAdmin ;

          13.? //共享對象的寫入會在應(yīng)用程序退出時候自動進行

          14.? //如果需要強制寫入,可以使用flush方法

          15.? myLSO . flush ();

          ?


          下面我們來看下在 Flex 中的一個簡單的本地共享對象應(yīng)用。用戶點擊按鈕后立即把在文本輸入框中的字符保存到本地共享對象中,第二次運行的時候就會在文本框中顯示上次保存的數(shù)據(jù)

          < mx : Application ? xmlns : mx = 'http://www.macromedia.com/2003/mxml' ? initialize = 'initApp();' >

          2. ?? < mx : Script ? source = 'LSO_script.as' />

          3. ?? < mx : TextInput ? id = 'myTI' />

          4. ?? < mx : Button ? label = 'Set ? Value' ? click = 'setVal();' ? />

          5. </ mx : Application >
          LSO_script.as
          文件代碼如下:

          1. var ? v ; ?

          2. var ? myLSO : SharedObject ; ?

          4. function ? initApp () ? {
          5. ?? // ? 初始化本地共享對象 ?

          6. ?? myLSO ? = ? SharedObject . getLocal ( 'dataStorage' ); ?

          7. ?? if ? ( myLSO == null ) ? { ? ?

          8. ???? alert ( '無法創(chuàng)建本地共享對象' , 'Error' ); ?

          9. ??? } ? else ? { ?

          10.? ???? getVal ();

          11.? ??? }

          12.? ? }

          13.? ?

          14.? function ? getVal () ? { ?

          15.? ?? // ? 取得共享對象數(shù)據(jù)

          16.? ?? v ? = ? myLSO . data . val ;

          17.? ?? myTI . text ? = ? v ;

          18.? ? }

          19.? ?

          20.? function ? setVal () ? { ?

          21.? ?? // ? 保存共享對象數(shù)據(jù)

          22.? ?? v ? = ? myTI . text ;

          23.? ?? myLSO . data . val ? = ? v ;

          24.? ?? myLSO . flush ();

          25.? ? }

          ?

          復(fù)雜的應(yīng)用可以保存數(shù)組對象到本地共享對象

          1. < mx : Application ? xmlns : mx = 'http://www.macromedia.com/2003/mxml' ? initialize = 'initApp();' >

          2. ?? < mx : Script ? source = 'LSO2_script.as' />

          3. ?? < mx : VBox ? backgroundColor = 'white' ? borderStyle = 'solid' ? marginLeft = '10' ? marginBottom = '10' ? width = '150' >

          4. ?? < mx : Label ? text = 'Color' />

          5. ?? < mx : TextInput ? id = 'myColor' ? width = '100' ? />

          6. ?? < mx : Label ? text = 'Scent' />

          7. ?? < mx : TextInput ? id = 'myScent' ? width = '100' ? />

          8. ?? < mx : Label ? text = 'Height' />

          9. ?? < mx : TextInput ? id = 'myHeight' ? width = '30' ? />

          10.? ?? < mx : Label ? text = 'Last ? SetVal ? On' />

          11.? ?? < mx : TextArea ? id = 'myLastDate' ? width = '100' ? height = '75' ? />

          12.? </ mx : VBox >

          13.? < mx : Button ? label = 'Set ? Values' ? click = 'setVal();' ? />

          14.? </ mx : Application >

          ?

          1. var ? myArray :Array;

          2. var ? myLSO : SharedObject ; ?

          4. function ? initApp () ? { ? ?

          5. ?? // ? 初始化本地共享對象

          6. ?? myLSO ? = ? SharedObject . getLocal ( 'flowerValues' );

          7. ?? if ? ( myLSO == null ) ? { ?

          8. ???? alert ( '無法創(chuàng)建本地共享對象' , 'Error' );

          9. ??? } ? else ? { ?

          10.? ???? getVal ();

          11.? ??? }

          12.? ? }

          13.? ?

          14.? function ? getVal () ? { ?

          15.? ?? // ? 取得共享對象的值

          16.? ?? myArray ? = ? myLSO . data . flowerArray ;

          18.? ?? myColor . text ? = ? myArray [ 0 ];

          19.? ?? myScent . text ? = ? myArray [ 1 ];

          20.? ?? myHeight . text ? = ? myArray [ 2 ];

          21.? ?

          22.? ?? myLastDate . text ? = ? myLSO . data . date ;

          23.? ? }

          24.? ?

          25.? function ? setVal () ? { ?

          26.? ?? //保存共享對象

          27.? ?? myArray [ 0 ] ? = ? myColor . text ;

          28.? ?? myArray [ 1 ] ? = ? myScent . text ;

          29.? ?? myArray [ 2 ] ? = ? myHeight . text ;

          30.? ?

          31.? ?? myLSO . data . flowerArray ? = ? myArray ;

          32.? ?? myLSO . data . date ? = ? new ? Date ();

          33.? ?? myLSO . flush ();

          34.? ? }

          posted on 2007-01-12 13:52 ???MengChuChen 閱讀(531) 評論(0)  編輯  收藏 所屬分類: flex2.0
          主站蜘蛛池模板: 墨江| 常德市| 探索| 巴林右旗| 沙田区| 乌鲁木齐市| 遂昌县| 丰宁| 华亭县| 十堰市| 绥中县| 金堂县| 徐水县| 崇义县| 息烽县| 奎屯市| 吉安市| 安远县| 鄢陵县| 长岛县| 泰来县| 龙井市| 牡丹江市| 南和县| 临高县| 怀集县| 济宁市| 特克斯县| 安乡县| 出国| 武胜县| 永顺县| 汾西县| 宁明县| 曲沃县| 平舆县| 普兰县| 宁城县| 桐庐县| 岑溪市| 海原县|