RIA*_*tar 8

有两种方法:一种使用绑定,更容易,另一种更复杂但性能更好.

使用绑定

假设您的视图类如下所示:

public class MyClass extends SkinnableComponent {
    [Bindable] public var myValue:String;
}
Run Code Online (Sandbox Code Playgroud)

那么你可以像这样绑定到皮肤中的那个值:

<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark">

    <fx:Metadata>
        [HostComponent("MyClass")]
    </fx:Metadata>

   <s:Label id="myLabel" text="{hostComponent.myValue}" />

</s:Skin>
Run Code Online (Sandbox Code Playgroud)

覆盖commitProperties

假设没有绑定的相同皮肤,则可以通过使用skinpart并覆盖主机组件中的commitProperties来设置标签的text属性.skinpart的名称必须与皮肤中组件的id完全相同(在本例中为'myLabel').

public class MyClass extends SkinnableComponent {

    [SkinPart(required="true")]
    public var myLabel:Label;

    public var myValue:String;

    override protected function commitProperties():void {
        if (myLabel) myLabel.text = myValue;
        super.commitProperties();
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,invalidateProperties()只要你想应用新值,你就必须打电话(例如在'myLabel'的setter函数中).另请注意,'myLabel'不再需要可绑定,除非您希望能够在外部绑定它.


编辑:选择哪种方法?

我刚刚回答了一个与这个问题密切相关的问题,其中我详细阐述了不同情况下每种方法的利弊.你可以在这里找到它:Flex:ButtonBarButton中的附加标签