當FLEX使用AC代碼的時候需要寫在FLEX的<mx:Script>標簽當中,
<mx:Script>
<![CDATA[
...
]]>
</mx:Script>
例如:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
var z:Number;
public function doSomething():void {
z = z + 1; // This must be in a function.
}
]]>
</mx:Script>
...
</mx:Application>
在ActionScript使用特殊的字符
在AC腳本當中可以假如特殊的字符,例如“<”,">"等和標簽相同的符號因為
<![CDATA[
...
]]>
內容內的代碼,不被XML進行解析,所以這些特殊字符理所當然的可以自由使用。這中方法也常在FLASH調用XML時候使用。
在ActionScript同FLEX組件關聯
可以在ActionScript中獲取FLEX的值,使ActionScript與FLEX進行交互,例如下面的例子
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:TextArea id="ta1" text="Congratulations. You are a winner."/>
</mx:Application>
在ActionScript中調用FLEX組件的值,使用如下代碼:
<mx:Script>
<![CDATA[
var str:String = ta1.text;
]]>
</mx:Script>
包含 ActionScript 代碼,導入ActionScript類
<mx:Script source="filename"></mx:Script> 其中filename是文件名,文件名可以是*.as也可以是一般文件。
例如:
loadas.mxml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="500">
<mx:Script source="loaded.as">
</mx:Script>
<mx:Panel title="loadAs" width="300" height="200" verticalAlign="middle" horizontalAlign="center">
<mx:Label id="mylabel" width="100" height="100" fontSize="14"/>
<mx:Button id="myButton" label="click me!" click="clickHandler(event);"/>
</mx:Panel>
</mx:Application>
loaded.as代碼如下:
// ActionScript file
import flash.events.MouseEvent;
private function clickHandler(event:MouseEvent):void{
mylabel.text = "Hello World!";
}
創建
ActionScript 組件
創建一個MyButton.as文件代碼如下:
// ActionScript file
package myControls
{
import mx.controls.Button;
public class MyButton extends Button {
public function MyButton() {
this.label = "Click Me!";
}
}
}
在FLEX中調用實現自己定義的組件,代碼如下:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cmp="myControls.*"
>
<cmp:MyButton />
</mx:Application>
}
posted on 2007-01-12 20:38
???MengChuChen 閱讀(356)
評論(0) 編輯 收藏 所屬分類:
flex2.0