在自定义 MXML 组件中分配子组件属性的 MXML 语法是什么?
我正在开发一个自定义 Flex 4 组件,它是两个现有 Flex 组件的聚合。我希望能够为组件指定我自己的自定义属性,并通过 MXML 访问现有的公共子组件属性。例如,我可能想调整标签和文本输入的字体颜色或样式。
一个聚合标签和文本输入的玩具组件:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
>
<fx:Script>
<![CDATA[
[Bindable] public var prompt:String = "default prompt";
[Bindable] public var input:String = "default inpput";
]]>
</fx:Script>
<s:VGroup>
<s:Label id="cLabel" text="{prompt}" />
<s:TextInput id="cTextInput" text="{input}" />
</s:VGroup>
</s:Group>
然后在我的主应用程序中,我想通过 mxml 访问子组件的公共接口,而无需为每个组件重新编写传递绑定。类似于:
...
<local:myInput prompt="name" input="please enter name">
<local:cLabel color="0xffffff" />
<local:CTextInput fontStyle="bold" />
</local:myInput>
在 ActionScript 中,人们可以轻松地对所有公共属性执行此操作:
myInput.cLabel.color = "0xffffff";
但我对 MXML 的语法感到困惑。这看起来应该很容易,但我还没有找到答案。非常感谢任何帮助。
I am working on a custom Flex 4 component which is an aggregation of two existing flex components. I would like to be able to specify my own custom properties for the component as well as access the existing public subcomponent properties via MXML. For instance I might want to adjust the font color or style for the label and text input.
A toy component which aggregates both a label and a text input:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
>
<fx:Script>
<![CDATA[
[Bindable] public var prompt:String = "default prompt";
[Bindable] public var input:String = "default inpput";
]]>
</fx:Script>
<s:VGroup>
<s:Label id="cLabel" text="{prompt}" />
<s:TextInput id="cTextInput" text="{input}" />
</s:VGroup>
</s:Group>
Then in my main application I would like to access the public interfaces of the sub-component via mxml without re-writing a pass-through binding for every one. Something like:
...
<local:myInput prompt="name" input="please enter name">
<local:cLabel color="0xffffff" />
<local:CTextInput fontStyle="bold" />
</local:myInput>
In actionscript one can do this easily for all public properties:
myInput.cLabel.color = "0xffffff";
But I am stumped on the syntax for MXML. This seems like it should be easy, but I have not found the answer yet. Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用 MXML 标记/值以菊花链方式连接显示层次结构。您可以按照您指定的方式在 ActionScript 中执行此操作,但即使这样也可能被认为是一种不好的做法。
我会指出颜色 和 fontStyle 不是属性。它们是样式所以,您拥有的代码:
很可能会抛出错误,因为颜色不是属性。您必须使用这样的代码:
但是,由于样式通常由子级继承;我怀疑在顶层组件中,您可以设置样式,它会立即自动渗透到子组件中。因此,您应该能够执行以下操作:
或者在 MXML 中:
并且它应该向下滴落。如果您想在子组件上单独设置样式,事情会变得更加棘手。
但是,回到您最初关于属性的问题。为了保持组件的封装性,您应该创建在子组件上设置的属性。像这样的事情:
如果您需要对很多属性执行此操作,那可能会很糟糕。如果该组件的封装不是优先考虑的;只需在 ActionScript 中设置它们即可。
You can't daisy chain down an display hierarchy w/ the MXML tag/value. You can do it in ActionScript, as you specified, but even that would probably be considered a bad practice.
I'll point out that color on the Label and fontStyle on the TextInput are not properties. They are styles So, the code you have:
Would most likely throw an error because color is not a property. You'd have to use code like this:
However, since styles are usually inherited by the children; I suspect at the top level component, you can set the style and it would immediately trickle through to the children automatically. So, you should just be able to do:
Or in MXML:
And it should trickle on down. Things can get trickier if you want to set styles individually on child components.
But, back to your original question w/ regards to properties. To keep a component encapsulated, you should create properties that set on the children. Something like this:
It can suck if you need to do this for a lot of properties. If encapsulation of this component isn't a priority; just set them in ActionScript.