Actionscript 3 更改Actionscript中SkinnableComponent的组件属性

Actionscript 3 更改Actionscript中SkinnableComponent的组件属性,actionscript-3,flex4,Actionscript 3,Flex4,Flex4将可视组件分离到皮肤中。那么,我们如何从可蒙皮组件访问这些可视元素呢?这是我的密码: <?xml version="1.0" encoding="utf-8"?> <s:SkinnableComponent xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" skinClass="skins.brushedSkin" xmlns:mx="librar

Flex4将可视组件分离到皮肤中。那么,我们如何从可蒙皮组件访问这些可视元素呢?这是我的密码:

<?xml version="1.0" encoding="utf-8"?>
<s:SkinnableComponent xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"  skinClass="skins.brushedSkin"
xmlns:mx="library://ns.adobe.com/flex/mx">


<fx:Script>
<![CDATA[
import mx.controls.TextInput;

private var txt:String;

public function setText(s:String) {
txt = s;
// the following line doesn't work
var input:TextInput = this.skin.getChildByName("msg") as TextInput;
input.text = s;
}

]]>
</fx:Script>


</s:SkinnableComponent>


首先,您必须在SkinnableComponent中指定所谓的Designer-Developer合同。 然后,您应该等待组件完成实例化,以便访问其皮肤部分

在您的特定情况下,您可以通过以下方式更改代码:

<?xml version="1.0" encoding="utf-8"?>
<s:SkinnableComponent xmlns:fx="http://ns.adobe.com/mxml/2009"
     xmlns:s="library://ns.adobe.com/flex/spark"  skinClass="skins.brushedSkin"
     xmlns:mx="library://ns.adobe.com/flex/mx">


<fx:Script>
<![CDATA[
import mx.controls.TextInput;

[SkinPart(required="true")]
public var input:TextInput;

private var txt:String;

public function setText(s:String) {
    txt = s;
    if (initialized)
        input.text = txt;
} 
]]>
</fx:Script>

</s:SkinnableComponent>

然后确保您的皮肤类包含以下声明(可能您只需要将msg-TextInput重命名为input):


SkinnableComponent是一个基类,没有自己的皮肤。从SkinnableComponent派生的类将具有自己的皮肤,例如TextInput。在上述情况下,您需要为自定义组件创建自己的自定义外观,并通过CSS或在运行时分配它。我不知道你想在这里做什么,也许你可以提供更多的背景?看起来您正试图创建一个具有TextInput的自定义组件,我关闭了吗?这是正确的方向,但最好使用getter/setter修改属性,并演示如何重写
partAdded
函数。这样你就可以摆脱不必要的条款“你应该等待你的组件完成它的实例化,以便访问它的皮肤部分”。好吧,最初我认为需要给出一个完整的解决方案,包括整个Flex组件生命周期(使用setter、invalidateproperties、commitProperties等,在这样的示例中不需要重写partAdded)。但后来我意识到,问题不在于如何以Flex方式设置属性,而在于如何在皮肤中访问特定的TextInput。