如何为自定义 Flex 组件定义默认样式?
我正在创建一个新的 Flex 组件 (Flex 3)。 我希望它有一个默认样式。 我的 .cs 文件是否有命名约定或其他内容以使其成为默认样式? 我错过了什么吗?
I'm creating a new Flex component (Flex 3). I'd like it to have a default style. Is there a naming convention or something for my .cs file to make it the default style? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Christian 关于应用 CSS 的说法是正确的,但如果您计划在跨项目的库中使用该组件,您将需要为该库编写一个默认的 css 文件。 操作方法如下:
这就是 Adobe 团队设置所有默认样式的方式,现在您也可以这样做。 刚刚发现这个-巨大
Christian's right about applying the CSS, but if you're planning on using the component in a library across projects, you're gonna want to write a default css file for that library. Here's how you do it:
That's how the Adobe team sets up all their default styles, now you can do it too. Just figured this out- huge
一般有两种方式。 一种方法是直接引用类名 - 例如,如果您在 ActionScript 中创建了一个新的组件类
MyComponent
,或者通过创建一个扩展另一个名为MyComponent< 的 UIComponent 的 MXML 组件来间接引用/code>,在这两种情况下,组件都会选择外部样式表中声明的样式,前提是样式表已导入到您的应用程序中(例如,通过
Style source
):另一种方法是设置UIComponent 的
styleName
属性(作为字符串):...并像这样在 CSS 文件中定义样式(注意点符号):
有意义吗?
Two ways, generally. One's just by referencing the class name directly -- so for example, if you'd created a new component class
MyComponent
in ActionScript, or indirectly by making an MXML component extending another UIComponent calledMyComponent
, in both cases, the component would pick up the styles declared in your external stylesheet, provided that stylesheet's been imported into your application (e.g., viaStyle source
):Another way is by setting the UIComponent's
styleName
property (as a string):... and defining the style in the CSS file like so (note the dot notation):
Make sense?
除了 Christian Nunciato 建议之外,另一个选择是为 Flex 组件的样式定义静态初始值设定项。 这允许您设置默认样式,而无需开发人员包含 CSS 文件。
In addition to what Christian Nunciato suggested, another option is to define a static initializer for your Flex component's styles. This allows you to set the default styles without requiring the developer to include a CSS file.
对 joshtynjala 建议的改进:
我在某处的文档中读过此内容; classContruct 方法会被自动调用。
A refinement of what joshtynjala suggested:
I've read this in the docs somewhere; the classContruct method gets called automatically.
您可能希望使用
标签或类似标签覆盖默认样式。 如果是这种情况,则在检查 classConstructed 时 CSSStyleDeclaration 可能已经存在。 这是一个解决方案:You may want to override default styles using the
<fx:Style>
tag or similar. If that's the case, a CSSStyleDeclaration may already exist by the time classConstructed is checked. Here's a solution:要创建默认样式,您还可以在类中拥有一个属性并覆盖 UIComponent 中的 styleChanged() 函数,例如仅在组件宽度的一半上绘制背景颜色:
您还可以为 customBackgroundColor 创建一个 setter ,该 setter 称为invalidateDisplayList(),因此您还可以通过编程方式以及通过 css 设置 customBackgroundColor 属性。
To create a default style you can also have a property in your class and override the styleChanged() function in UIComponent, eg to only draw a background color across half the width of the component:
You could also create a setter for the customBackgroundColor that called invalidateDisplayList(), so you could also set the customBackgroundColor property programatically as well as through css.