在 Flex 4 中使用 AS3 实例化按钮?
我在 Flex 3.5 SDK 和 Flash Builder 4 上使用了这段代码,该代码在 MXML 应用程序的creationComplete 上调用的函数内。
var myButton:Button = new Button;
myButton.label = "test";
addChild(myButton);
Alert.show("Button Created");
然而,它工作正常,当我仅在同一个 Flash Builder 4 上使用它时,在 Flex 4.0 SDK 下,什么也没有发生。 Alert.show() 甚至没有显示,这意味着它甚至没有达到这一点。
所以我的问题是,出了什么问题?我错过了什么吗?
聚苯乙烯 我需要能够动态创建/删除 MXML 组件(在应用程序运行时)。这只是一个测试脚本,我在实现我所需要的方面惨遭失败。
I used this code on Flex 3.5 SDK with Flash Builder 4 inside a function that is being called on creationComplete of the MXML app.
var myButton:Button = new Button;
myButton.label = "test";
addChild(myButton);
Alert.show("Button Created");
It works fine however, when I use it on the same Flash Builder 4 only this time, under Flex 4.0 SDK, nothing's happening. The Alert.show() isn't even showing which means it doesn't even get to that point.
So my question is, what's wrong? Am I missing something?
P.S.
I need to be able to create / remove MXML components on the fly (while the app is running). This is just a test script and I'm failing miserably at achieving what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Spark 中,您需要使用 addElement 而不是 addChild
我不确定为什么您的警报不起作用:\
In Spark you need to use addElement instead of addChild
I'm not sure why your alert isn't working :\
通常,您在 MXML 中创建 UI,但在某些情况下,您确实需要动态创建 UI 元素,正如另一张海报提到的,addElement() 是 Flex 4 Spark 容器的关键。
将组件添加到 MX 容器(从 Flex 3 开始)时,您仍然在 Flex 4 中使用 addChild()。仅在添加到 Spark 容器时才需要使用 addElement()。
当然,当有类似的 MX 容器时,Adobe 建议您使用 Spark 容器。
Usually you create the UI in MXML, but in some cases you do need to create UI elements on the fly, and as the other poster mentioned, addElement() is the key with Flex 4 Spark containers.
When adding components to MX containers (from Flex 3), you still use addChild() in Flex 4. You only need to use addElement() when adding to Spark containers.
Of course, Adobe recommends you use the Spark containers when there is a somewhat comparable MX container.
最好以灵活的方式进行。
您不能仅使用 Flex 中的 addChild 添加按钮。首先,您需要创建一个 UIComponent,然后将按钮添加到 UIComponent。和flash的方式有点不一样。
Its better that you do it in the flex way.
You can't add a button using mere addChild in flex. First you need to create an UIComponent and then add the button to the UIComponent. Its a bit different from the flash way.