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

          基本的使用方法如下:

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

          2. var ? myLSO : SharedObject ; ?

          3. //創建本地共享對象 ?

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

          5. //給共享對象賦值

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

          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.? //共享對象的寫入會在應用程序退出時候自動進行

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

          15.? myLSO . flush ();

          ?


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

          < 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 ( '無法創建本地共享對象' , 'Error' ); ?

          9. ??? } ? else ? { ?

          10.? ???? getVal ();

          11.? ??? }

          12.? ? }

          13.? ?

          14.? function ? getVal () ? { ?

          15.? ?? // ? 取得共享對象數據

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

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

          18.? ? }

          19.? ?

          20.? function ? setVal () ? { ?

          21.? ?? // ? 保存共享對象數據

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

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

          24.? ?? myLSO . flush ();

          25.? ? }

          ?

          復雜的應用可以保存數組對象到本地共享對象

          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 ( '無法創建本地共享對象' , '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
          主站蜘蛛池模板: 陈巴尔虎旗| 廉江市| 桐梓县| 德化县| 云和县| 长乐市| 泽普县| 乡宁县| 邵阳县| 望谟县| 大安市| 济宁市| 林芝县| 江山市| 古蔺县| 都匀市| 环江| 无极县| 马公市| 绿春县| 贵定县| 郎溪县| 滁州市| 克东县| 藁城市| 平安县| 黑水县| 达州市| 舒城县| 九龙城区| 璧山县| 汤原县| 桐梓县| 西平县| 勐海县| 内丘县| 和龙市| 泗水县| 湛江市| 醴陵市| 遵义县|