虽然将 [Style ...] 元数据标签添加到我的子类确实使属性 showPromptWhenFocused 可从 MXML 访问,但 initializeStyles() 函数没有成功将默认值更改为 true。
我希望用户能够根据需要将 showPromptWhenFocus 设置为 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);
尝试覆盖 notifyStyleChangeInChildren()。
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);
}
}