在
Flash
中提供了下面的操作本地對象的方法:
SharedObject.clear()
刪除本地共享對象
SharedObject.flush()
立即把共享對象數(shù)據(jù)寫入本地文件
SharedObject.getLocal()
創(chuàng)建或連接本地共享對象
SharedObject.getSize()
取得本地共享對象的指定大小數(shù)據(jù)
Flex
中使用本地共享對象的方法本質上和在
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.?
//共享對象的寫入會在應用程序退出時候自動進行
14.?
//如果需要強制寫入,可以使用flush方法
15.?
myLSO
.
flush
();
?
下面我們來看下在
Flex
中的一個簡單的本地共享對象應用。用戶點擊按鈕后立即把在文本輸入框中的字符保存到本地共享對象中,第二次運行的時候就會在文本框中顯示上次保存的數(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.?
?
}
?
復雜的應用可以保存數(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 閱讀(525)
評論(0) 編輯 收藏 所屬分類:
flex2.0