【发布时间】:2011-02-09 10:22:32
【问题描述】:
我已经实现了一个 MXML 自定义组件,我想将参数传递给构造函数:
newUser = new userComp("name");
而不是使用 set 方法。
如果自定义组件是在 MXML 中构建的(使用initialize=myPseudoCostructor() 方法),这可能吗?
或者我只能通过额外的代码行来设置参数?
【问题讨论】:
标签: apache-flex custom-component
我已经实现了一个 MXML 自定义组件,我想将参数传递给构造函数:
newUser = new userComp("name");
而不是使用 set 方法。
如果自定义组件是在 MXML 中构建的(使用initialize=myPseudoCostructor() 方法),这可能吗?
或者我只能通过额外的代码行来设置参数?
【问题讨论】:
标签: apache-flex custom-component
您不能pass variables 进入MXML 组件的构造函数。我什至不相信 define a constructor 在 MXML 组件中是可能的,尽管我可能是错的。但是,您可以设置具有默认值并在构建后更改的属性。
您还可以创建一个初始化函数,该函数也在构造后调用。
【讨论】:
Multiple constructor definitions found. Constructor may not be defined in <Script /> code. 它说here(在注释中)这是明确禁止的。
IN COMPONENT (cosa)
<fx:Declarations>
<fx:String id="name">Jon Doe</String>
</fx:Declarations>
<fx:Script>
trace("hola "+name)
</fx:Script>
<s:Label text="Hola {name}"/>
IMPLEMENT
<cosa name="Juan Perez"/>
【讨论】:
我最近遇到了这个问题 - 你可以做的是创建一个返回组件的初始化函数:
在组件内部(为了论证,称为 MyComponent):
public function init(...args):MyComponent {
//Add constructor code here
return this;
}
...创建组件时,可以这样调用:
var myComp:MyComponent = new MyComponent().init(args);
当它返回对象时,您可以将其视为构造函数。最近经常用这种方法,看来是解决问题的最好方法了。
【讨论】: