在 actionScript 中将元素添加到移动应用程序的视图
我有一个带有项目列表的程序。项目是一个扩展View 的actionScript 类。当我单击列表时,它会推送视图。在此类的构造函数中,我添加了一些按钮,并且有一个添加另一个按钮的函数。 我的问题是视图仅显示构造函数中创建的按钮,而不显示函数中创建的按钮。
类
package
{
import spark.components.Button;
public class Application extends View
{
public function Application()
{
var bt:Button = new Button();
bt.label = "In C";
addElement(bt);
}
public function addButton():void {
var b:Button = new Button();
b.label = "Olé";
addElement(b);
visible = true;
}
}
}
FirstView
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="Home"
visible="false" creationComplete="retrieveApplication(event)">
<fx:Script>
<![CDATA[
import ...
protected function retrieveApplication(event:FlexEvent):void
{
...
var application:Application = new Application();
...
application.addButton();
this.visible = true;
}
protected function launchApplication(event:IndexChangeEvent):void
{
navigator.pushView(Application);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="10" y="10" width="460" label="Button" click="launchApplication(event)"/>
我做错了吗?
I have a program with a list of item. An item is an actionScript class that extends View. When I click on the list, it push the view. In the constructor of this class I add some buttons and I have a function that add another button.
My problem is that the view display only the button create in the constructor and not the one create in the function.
The class
package
{
import spark.components.Button;
public class Application extends View
{
public function Application()
{
var bt:Button = new Button();
bt.label = "In C";
addElement(bt);
}
public function addButton():void {
var b:Button = new Button();
b.label = "Olé";
addElement(b);
visible = true;
}
}
}
The firstView
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="Home"
visible="false" creationComplete="retrieveApplication(event)">
<fx:Script>
<![CDATA[
import ...
protected function retrieveApplication(event:FlexEvent):void
{
...
var application:Application = new Application();
...
application.addButton();
this.visible = true;
}
protected function launchApplication(event:IndexChangeEvent):void
{
navigator.pushView(Application);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="10" y="10" width="460" label="Button" click="launchApplication(event)"/>
That did I do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您传递的是对 PushView 的类引用,而不是实例。这意味着您不会传递在retrieveApplication 方法中创建的应用程序实例。
在调用pushView 后,Flex 会实例化自己的视图实例。所以 addButton 方法永远不会被调用。
Notice that you pass a class reference to the pushView , not an instance. That means you don't pass the application instance you created in the retrieveApplication method.
Flex instanciates itself an instance of the view after you call pushView. So the addButton method is never called.