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

首頁 > 網站 > WEB開發 > 正文

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

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

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

這里提供的類很簡單,但它可根據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;
}
}
}

設置好targetFactory 后,AddChild 方法根據SpecialDeferredInstance 實例的type 參數可有不同類型的對象,例如:
+展開
-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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江安县| 香港| 清流县| 阜城县| 湟中县| 容城县| 无锡市| 柘城县| 桂平市| 霍邱县| 奉化市| 延庆县| 中卫市| 云安县| 肥东县| 紫阳县| 溆浦县| 太原市| 上栗县| 福泉市| 大同市| 重庆市| 仁化县| 牡丹江市| 邵阳县| 绵竹市| 西盟| 万荣县| 招远市| 钦州市| 湘乡市| 安溪县| 曲阜市| 叙永县| 郯城县| 上杭县| 博乐市| 昆明市| 余江县| 师宗县| 崇文区|