Assign variable to MXML component ID

本小妞迷上赌 提交于 2019-12-07 23:58:17

问题


I have my custom component and for example few Label. I want to pass to my component value which will be assign to label's id.

Code:

<fx:Script>
        <![CDATA[
            [Inspectable]
            [Bindable]
            public var test:String = "asd";
        ]]>
</fx:Script>
<s:Label id="{test}" text="etc"/>

Error: {test} is not a valid identifier

Can I even do something like that?


回答1:


No you can't. You have to understand that when you write an mxml component like

<s:Group>
    <s:Label id="myLabel" />
</s:Group>

it will generate ActionScript code like

public class MyClass extends Group {
    public var myLabel:Label;
}

(Mind you, I grossly oversimplify the code here to convey the most important part).

As you can see your 'id' is in fact a property name. And you can't change a property's name at runtime can you?



来源:https://stackoverflow.com/questions/6533585/assign-variable-to-mxml-component-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!