问题描述
是不是有一些方法可以重写下面的代码,这样我就不需要每一个可以想象的类型的巨大的switch语句了?另外,如果我可以用某种方式替换switch语句来动态地创建新的控件,那么我可以使代码更小,更直接,而且不必预测自定义控件类型的可能性。
之前:
<?xml version =1.0encoding =utf-8 ?>
layout =vertical>
< mx:Script>
<![CDATA [
import mx.containers.HBox;
导入mx.controls.Button;
导入mx.controls.Label;
public function CreateControl(event:Event):void {
var Type:String = Edit.text;
var NewControl:Object;
switch(Type){
'mx.controls :: Label':NewControl = new Label(); break;
case'mx.controls :: Button':NewControl = new Button(); break;
case'mx.containers :: HBox':NewControl = new HBox(); break;
...所有其他类型,包括不可预见的自定义类型
$ b this.addChild(NewControl as DisplayObject);
}
]]>
< / mx:Script>
< mx:TextInput id =Edit/>
< mx:Button label =Createclick =CreateControl(event);/>
< / mx:WindowedApplication>
后:
<?xml version =1.0encoding =utf-8?>
layout =vertical>
< mx:Script>
<![CDATA [
import mx.containers.HBox;
导入mx.controls.Button;
导入mx.controls.Label;
public function CreateControl(event:Event):void {
var Type:String = Edit.text;
var NewControl:Object = * ??? *(Type);
this.addChild(NewControl as DisplayObject);
}
]]>
< / mx:Script>
< mx:TextInput id =Edit/>
< mx:Button label =Createclick =CreateControl(event);/>
< / mx:WindowedApplication>
我还没有运行这个代码,但是你应该可以按照
<$ p公共函数CreateControl(event:Event):void {
var Type:String = Edit.text;
var controlClass:Class = getDefinitionByName(Type)as Class;
var NewControl:Object = new controlClass();
this.addChild(NewControl as DisplayObject);
}
Isn't there some way to re-write the following code, such that I don't need a gigantic switch statement with every conceivable type? Also, if I can replace the switch statement with some way to dynamically create new controls, then I can make the code smaller, more direct, and don't have to anticipate the possibility of custom control types.
Before:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Script>
<![CDATA[
import mx.containers.HBox;
import mx.controls.Button;
import mx.controls.Label;
public function CreateControl(event:Event):void {
var Type:String=Edit.text;
var NewControl:Object;
switch (Type) {
case 'mx.controls::Label':NewControl=new Label();break;
case 'mx.controls::Button':NewControl=new Button();break;
case 'mx.containers::HBox':NewControl=new HBox();break;
... every other type, including unforeseeable custom types
}
this.addChild(NewControl as DisplayObject);
}
]]>
</mx:Script>
<mx:Label text="Control Type"/>
<mx:TextInput id="Edit"/>
<mx:Button label="Create" click="CreateControl(event);"/>
</mx:WindowedApplication>
AFTER:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Script>
<![CDATA[
import mx.containers.HBox;
import mx.controls.Button;
import mx.controls.Label;
public function CreateControl(event:Event):void {
var Type:String=Edit.text;
var NewControl:Object= *???*(Type);
this.addChild(NewControl as DisplayObject);
}
]]>
</mx:Script>
<mx:Label text="Control Type"/>
<mx:TextInput id="Edit"/>
<mx:Button label="Create" click="CreateControl(event);"/>
</mx:WindowedApplication>
Take a look at flash.utils.getDefinitionByName().
I haven't run this code, but you should be able to do something along the lines of
public function CreateControl(event:Event):void {
var Type:String=Edit.text;
var controlClass:Class = getDefinitionByName(Type) as Class;
var NewControl:Object= new controlClass();
this.addChild(NewControl as DisplayObject);
}
这篇关于在ActionScript中动态创建Flex组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!