?问题很简单,我扩展Button 来创建ImageButton,我扩展ButtonSkin 来创建ImageButtonSkin。但是,当我这样定义 hostComponent 时:
public var hostComponent:ImageButton
我收到一个错误,提示 ButtonSkinBase 中的 hostComponent:ButtonBase 存在冲突。
如何扩展皮肤并为其提供新的 hostComponent?
?问题很简单,我扩展Button 来创建ImageButton,我扩展ButtonSkin 来创建ImageButtonSkin。但是,当我这样定义 hostComponent 时:
public var hostComponent:ImageButton
我收到一个错误,提示 ButtonSkinBase 中的 hostComponent:ButtonBase 存在冲突。
如何扩展皮肤并为其提供新的 hostComponent?
如何扩展皮肤并为其提供新的 hostComponent?
为什么必须这样做?hostComponent 是一个 ButtonBase,它是您的 ImageButton 扩展的东西。为什么需要更改 hostComponent 的类型?如果您需要将 hostComponent 作为 ImageButton 访问,则可以对其进行强制转换:
var myImageButton : ImageButton = hostComponent as ImageButton;
在对这个问题的评论中,您说:
所以,你可以扩展皮肤,但你只能将它用于父皮肤指定的组件,太棒了。
这根本不应该是真的。您说自己的 hostComponent 具有 ButtonBase 类型,但您正在扩展 ButtonSkin。根据您的说法,您将无法在 Button 组件上使用 ButtonSkin,只能在 ButtonBase 组件上使用。那不是真的。
我还将为@RIAstar 关于元数据的评论添加一项说明。元数据仅用于编译器的代码提示和 MXML 参数检查。您可以轻松地创建一个没有这些的样式。您只需使用 setStyle 方法设置值:
myHostComponent.setStyle('myImageColor',0x000000);
你可以像这样在皮肤中使用它:
this.getStyle('myImageColor');
我认为 - 但不确定 - 即使未声明元数据,您也可以通过 CSS 设置样式。使用 Spark 组件生命周期,在组件初始化其样式之前设置样式应该是完全可能的。