国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

11.8.使用States的延時實例工廠

2024-04-27 13:52:11
字體:
供稿:網(wǎng)友
11.8.1. 問題
我需要一個對象,它能為AddChild 對象實例化不同類型的對象。
11.8.2. 解決辦法
創(chuàng)建工廠類,并賦值給AddChild 對象的targetFactory 屬性。
11.8.3. 討論
AddChild 對象的targetFactory 屬性需要一個實現(xiàn)IDeferredInstance 接口的對象。

IDeferredInstance 接口只需要一個方法:getInstance():Object. 當AddChild 對象需要一個新的可視化對象被添加到組件時該方法返回所需的實例對象。

這里提供的類很簡單,但它可根據(jù)type 屬性值返回不同類型的UIComponent:
+展開
-ActionScript
package oreilly.cookbook
{
import mx.containers.HBox;
import mx.containers.VBox;
import mx.controls.Button;
import mx.controls.Text;
import mx.controls.TextInput;
import mx.core.IDeferredInstance;
import mx.core.UIComponent;
public class SpecialDeferredInstance implements
IDeferredInstance
{
private var comp:UIComponent;
private var _type:String;
public function set type(str:String):void {
_type = str;
}
public function get type():String{
return _type;
}
public function getInstance():Object
{
var text:Object;
if(_type == "TextVBox"){
comp = new VBox();
text = new Text();
text.text = "TEXT";
comp.addChild(text as Text);
var btn:Button = new Button();
btn.label = "LABEL";
comp.addChild(btn);
comp.height = 160;
comp.width = 320;
}
else
{
comp = new HBox();
text = new TextInput();
text.text = "TEXT";
comp.addChild(text as TextInput);
var btn:Button = new Button();
btn.label = "LABEL";
comp.addChild(btn);
comp.height = 160;
comp.width = 320;
}
return comp;
}
}
}

設(shè)置好targetFactory 后,AddChild 方法根據(jù)SpecialDeferredInstance 實例的type 參數(shù)可有不同類型的對象,例如:
+展開
-XML
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxmlwidth="400"
height="300currentState="empty">

<mx:Script>
<![CDATA[
import oreilly.cookbook.SpecialDeferredInstance;
[Bindable]
private var defInst:SpecialDeferredInstance =
new SpecialDeferredInstance();

]]>
</mx:Script>
<mx:states>
<mx:State name="defInst">
<mx:AddChild relativeTo="{mainHolder}"
targetFactory="{defInst}"/>

</mx:State>
<mx:State name="empty"/>
</mx:states>
<mx:Button click="currentState == 'defInst' ? currentState='empty' : currentState='defInst'label="change"/>
<mx:HBox id="mainHolder"/>
</mx:VBox>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五莲县| 资源县| 永嘉县| 囊谦县| 泰安市| 星子县| 尼玛县| 遵义县| 宿州市| 原阳县| 南丹县| 阳东县| 湘阴县| 石棉县| 合川市| 蓝山县| 肇源县| 广汉市| 辽源市| 扶风县| 南平市| 偃师市| 玛纳斯县| 南康市| 田东县| 安宁市| 托克逊县| 河间市| 平湖市| 肥乡县| 长沙市| 郑州市| 开化县| 手机| 兴仁县| 呼和浩特市| 积石山| 广灵县| 台南县| 阜康市| 腾冲县|