将变量分配给 MXML 组件 ID [英] Assign variable to MXML component ID

查看:46
本文介绍了将变量分配给 MXML 组件 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的自定义组件,例如几个标签.我想传递给我的组件值,该值将分配给标签的 id.

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.

代码:

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

错误:{test} 不是有效标识符

Error: {test} is not a valid identifier

我什至可以做这样的事情吗?

Can I even do something like that?

推荐答案

不,你不能.你必须明白,当你写一个像

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

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

它将生成类似的 ActionScript 代码

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).

如您所见,您的id"实际上是一个属性名称.而且您不能在运行时更改属性的名称,对吗?

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?

这篇关于将变量分配给 MXML 组件 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆