Flex3學習筆記4

來源:互聯網
上載者:User
Flex3學習筆記4

Flex指令碼基礎

一、ActionScript和MXML的關係
Flex由兩種語言群組成,ActionScript和MXML。MXML負責應用的布局結構,ActionScript負責建立互動。那麼,ActionScript和MXML有什麼異同呢?

1)MXML = ActionScript
MXML標記實際上能被Flex編譯器轉換成ActionScript。你可以這麼考慮:ActionScript是Flash Player的核心語言,Flex中的一切東西都能被提取成ActionScript。在這點上,你可以使用ActionScript項目建立一個Flex應用,且只使用ActionScript語言。然而,使用MXML具有更好的直觀性來建立應用程式。

2)標記都是類(Tags are Classes)
Flex應用程式中聲明的標記在編譯時間能被轉換成ActionScript的合適代碼。比如,要在MXML中建立一個Button按鈕,寫如下代碼:

  1. <mx:Button id="myButton" />

這能被編譯成等價的ActionScript代碼:

  1. import mx.controls.Button;
  2. var myButton:Button = new Button();
  3. addChild(myButton);

注意:你可以使用ActionScript來動態建立組件,而不是只依賴於MXML。

3)Attributes are Properties
當你為標籤增加屬性Attributes時,你實際上是在改變組件執行個體的properties。例如,要改變按鈕Button的label屬性,在MXML中如下:

  1. <mx:Button id="myButton" label="Click Me" />

在ActionScript等價的操作為:

  1. import mx.controls.Button;
  2. var myButton:Button = new Button();
  3. myButton.label = "Click Me";
  4. addChild(myButton);

4)屬性是樣式(Attributes are Styles)
一個組件有多種屬性,正如之前看到的屬性面板。Styles是Flex組件特殊的屬性,用於控制組件的顯示外觀。在MXML,可以很容易地使用屬性設定style。在ActionScript,可使用getStyle()和setStyle()方法。
例如:cornerRadius的style屬性用於設定按鈕,如下:

  1. <mx:Button id="myButton" cornerRadius="14" />

但不可以用指令碼直接設定,如:

  1. myButton.cornerRadius = 14;

而是應該用setStyle()方法,如下:

  1. myButton.setStyle("cornerRadius", 14);

5)屬性是事件監聽器(Attributes are event listeners)
事件監聽器用於告訴組件響應事件,如滑鼠點擊。如下:

  1. <mx:Button id="myButton" click="doSomething()" />

那就應該執行ActionScript指令碼:

  1. import mx.controls.Button;
  2. var myButton:Button = new Button();
  3. myButton.addEventListener("click", doSomething);
  4. addChild(myButton);

要注意,ActionScript是使用addEventListener()方法註冊事件的。

二、注釋
注釋文法在MXML和ActionScript中是不一樣的,
在ActionScript中是雙斜線“//"或”/* */",
而在MXML中是<!-- -->。
這正好滿足一為指令碼語言、一為XML語言的文法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.