Actionscript 3 我想覆盖从祖先组件继承的样式的默认值[flex4]

Actionscript 3 我想覆盖从祖先组件继承的样式的默认值[flex4],actionscript-3,apache-flex,styles,flex4,custom-component,Actionscript 3,Apache Flex,Styles,Flex4,Custom Component,将[Style…]元数据标记添加到我的子类确实使属性showPromptWhenFocused可从MXML访问,但initializeStyles函数没有成功地将默认值更改为true 我希望用户能够将showPromptWhenFocused设置为false(如果他们愿意),但我希望默认值为true package com.santacruzsoftware.crafting.controls { import mx.core.FlexGlobals; import mx.styles.CSSS

将[Style…]元数据标记添加到我的子类确实使属性showPromptWhenFocused可从MXML访问,但initializeStyles函数没有成功地将默认值更改为true

我希望用户能够将showPromptWhenFocused设置为false(如果他们愿意),但我希望默认值为true

package com.santacruzsoftware.crafting.controls
{
import mx.core.FlexGlobals;
import mx.styles.CSSStyleDeclaration;
import mx.styles.StyleManager;

import spark.components.TextInput;

[Style(name="showPromptWhenFocused", inherit="yes", type="Boolean")]

public class WatermarkTextInput extends TextInput
{
    private static function initializeStyles() : void
    {
        var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("showPromptWhenFocused");
        if (!style)
            style = new CSSStyleDeclaration();

        style.defaultFactory = function() : void
        {
            this.showPromptWhenFocused = true;
        }

        FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("showPromptWhenFocused", style, false);
    }
    //call the static function immediately after the declaration
    initializeStyles();
}
} 
有什么想法吗?

当您以类的名称调用getStyleDeclaration pass时,您可以获得WaterMarkTextInput样式声明:

var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("WatermarkTextInput");
然后在为类设置样式时执行相同的操作:

FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("WatermarkTextInput", style, false);
尝试覆盖


请记住,当您通过MXML实例化类时,defaultFactory旨在包含MXML中设置的默认值。因此,此工厂可能无法覆盖所有实例中的样式。我在重写factory而不是defaultFactory时得到了更好的结果。可能存在不包含style属性的声明,因此您可能希望检查您得到的声明中的值。另外,Flex4+中的所有UIComponents都引用了stylemanager。您不需要使用FlexGlobals,也不应该使用。Amy-initializeStyles是静态的,因此它无法访问实例中的任何样式管理器引用,否?如何允许祖先设置所有其他样式?或者,当子代的静态函数执行时,它是否已经这样做了?调试,我发现如果!style为true,就好像还没有设置任何样式一样;但它仍然没有覆盖默认值。我还尝试显式调用parentStyle.defaultFactory等。答案不应该只包含代码。为了使你的答案更有帮助,请解释你的代码和OP代码之间的区别,并解释为什么它工作得更好。对不起,我的英语很糟糕=我认为你的英语很好。
package com.santacruzsoftware.crafting.controls {

    import mx.core.FlexGlobals;
    import mx.styles.CSSStyleDeclaration;
    import mx.styles.StyleManager;

    import spark.components.TextInput;

    [Style(name="showPromptWhenFocused", inherit="yes", type="Boolean")]
    public class WatermarkTextInput extends TextInput {

        public var inheritStyles:boolean = true;

        private static function initializeStyles():void {
            var style : CSSStyleDeclaration = FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("showPromptWhenFocused");
            if (!style)
                style = new CSSStyleDeclaration();

            style.defaultFactory = function():void {
                this.showPromptWhenFocused = true;
            }

            FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("showPromptWhenFocused", style, false);
        }

        //call the static function immediately after the declaration
        initializeStyles();

        public override function notifyStyleChangeInChildren(styleProp:String, recursive:Boolean):void {
            if (!inheritStyles) {
                switch(styleProp) {
                    case 'showPromptWhenFocused':
                        return;
                }
            }
            super.notifyStyleChangeInChildren(styleProp, recursive);
        }

}