将自定义样式属性添加到 MXML 自定义组件

发布于 2024-11-16 15:23:37 字数 317 浏览 12 评论 0原文

我有一个自定义组件,它有几个画布,并为其分配了一些背景颜色。现在我已经对颜色进行了硬编码,我想将它们移动到外部 css 文件。

所以我想要这样的 css 声明:

ControlBar  
{  
  dividerRightColor: #ffffff;  
  dividerLeftColor: #f3f3f3;  
}

我的问题是我是否可以定义像 dividerRightColor 这样的自定义样式名称,如果可以,我如何在我的 MXML 组件中使用该值?我见过在 Pure AS 组件中使用它们的示例。

I have a Custom Component that has a couple of Canvas with some background colors assigned to them. Now i have hard coded the colors, i want to move them to an external css file.

So i would like to have the css declaration like this :

ControlBar  
{  
  dividerRightColor: #ffffff;  
  dividerLeftColor: #f3f3f3;  
}

My question is if i can define custom style names like dividerRightColor and if so, how can i use that value inside my MXML Component? I have seen examples of using them inside Pure AS components.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无风消散 2024-11-23 15:23:37

在 CSS 中:

.dividerRightColor {
    background-color: #ffffff;
}


.dividerLeftColor {
    background-color: #f3f3f3;
}

在 MXML 中:

<mx:ControlBar>
    <mx:Canvas styleName="dividerLeftColor">
        …
    </mx:Canvas>

    <mx:Canvas styleName="dividerRightColor">
        …
    </mx:Canvas>
</mx:ControlBar>

In CSS:

.dividerRightColor {
    background-color: #ffffff;
}


.dividerLeftColor {
    background-color: #f3f3f3;
}

In MXML:

<mx:ControlBar>
    <mx:Canvas styleName="dividerLeftColor">
        …
    </mx:Canvas>

    <mx:Canvas styleName="dividerRightColor">
        …
    </mx:Canvas>
</mx:ControlBar>
‘画卷フ 2024-11-23 15:23:37

在我看来,您需要在组件中创建样式;不仅仅是将样式值作为其他答案发送到组件中。

阅读此文档

基本上,样式的定义方式与属性的定义方式不同。您可以在所需的组件上设置任何样式名称。然而,组件需要知道如何使用样式。为此,您需要重写 styleChanged 方法:

override public function styleChanged(styleProp:String):void {

    super.styleChanged(styleProp);

    // Check to see if style changed. 
    if (styleProp=="dividerRightColor") 
    {
      // do stuff to implement the style
      dividerRight.setStyle('backgroundColor',getStyle('dividerRightColor'));
    }
}

常见的方法是设置“styleChanged”属性并使显示列表无效,然后在 updateDisplayList() 方法中进行适当的样式更改。

要使样式在代码提示中可用,您需要添加元数据,如下所示:

[Style(name="dividerRightColor")]

仅当您希望将样式设置为 MXML 中的属性时才需要这样做。

It sounds to me like you need to create the style in the component; not just send the style values into the component as the other answer.

Read this documentation.

Basically, styles don't get defined the same way that properties get defined. You can set any style name on the component you want. However, the component needs to know what to do w/ the style. To do that you need to override the styleChanged method:

override public function styleChanged(styleProp:String):void {

    super.styleChanged(styleProp);

    // Check to see if style changed. 
    if (styleProp=="dividerRightColor") 
    {
      // do stuff to implement the style
      dividerRight.setStyle('backgroundColor',getStyle('dividerRightColor'));
    }
}

A common approach is to set "styleChanged" properties and invalidate the display list and then make the appropriate style changes in the updateDisplayList() method.

To make the style available in code hinting, you'll need to add metadata, like this:

[Style(name="dividerRightColor")]

This will only be required if you wish to set the style as a property in MXML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文