软硬度:皮肤和数据模型之间通​​信?软硬、皮肤、数据模型

2023-09-08 14:33:52 作者:累却坚持i

如何发送到皮肤已改变了一些价值?

How to send to the skin some value which have changed?

推荐答案

有两种方法可以这样:人使用绑定和更容易,另一种是比较复杂的,但获得更好的性能。

There are two approaches to this: one uses binding and is easier, the other is more complex but better for performance.

使用结合

假设你的视图类看起来是这样的:

Suppose your view class looks like this:

public class MyClass extends SkinnableComponent {
    [Bindable] public var myValue:String;
}

然后就可以绑定到该值在你的皮肤是这样的:

then you can bind to that value in your skin like this:

<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>

覆盖的commitProperties

假设相同的皮肤没有绑定,那么你可以通过使用skinpart和压倒一切的commitProperties在主机组件上设置标签的文本属性。该skinpart的名字必须是完全一样的皮肤的组件的ID(在这种情况下,myLabel')。

Assume the same skin without the binding, then you can set the label's text property by using a skinpart and overriding commitProperties in the host component. The skinpart's name must be exactly the same as the id of the component in the skin (in this case '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();
    }
}

当然,你将不得不调用 invalidateProperties()每当你想(在二传手功能myLabel为例)应用新的价值。还要注意,myLabel不再需要被绑定,除非你想能够在其绑定到外部。

Of course you would have to call invalidateProperties() whenever you want to apply the new value (for instance in a setter function for 'myLabel'). Also notice that 'myLabel' no longer needs to be bindable, unless you would want to be able to bind on it externally.

编辑:选择哪种方法

我刚才已经回答了密切相关,这个在我阐述亲的在不同情况下每种方法的利弊问题。你可以在这里找到它:弹性:在ButtonBarButton 附加标签

I have just answered a question that is closely related to this one in which I elaborate on the pro's and cons of each approach in different situations. You can find it here: Flex: Additional label in ButtonBarButton