Flex 4 - 创建状态并在运行时从actionscript添加元素 [英] Flex 4 - Create states and add elements at runtime from actionscript

查看:92
本文介绍了Flex 4 - 创建状态并在运行时从actionscript添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态地(在运行时)添加新的状态到一个容器,在这个状态下添加不同的元素(如TextInput,Label等)。这必须从actionscript完成,我不使用任何mxml文件。我可以添加状态和更改不同元素的属性或样式,但我没有想出如何为不同的状态添加子元素。



谢谢。

>

Cristi

解决方案

您必须手动创建状态类并创建它的覆盖。然后将新创建的状态添加到组件的状态数组中。



下面是一个小例子,显示如何执行此操作:

 <?xml version =1.0encoding =utf-8?> 
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx>
< fx:Script>
<![CDATA [
import mx.events.FlexEvent;
导入mx.states.AddChild;
导入mx.states.AddItems;
导入mx.states.SetProperty;
导入mx.states.State;
$ b保护函数createState(event:MouseEvent):void
{
var label:Label = new Label();
label.text =世界!;

var addLabel:AddItems = new AddItems();
addLabel.relativeTo = foo;
addLabel.position =after;
addLabel.items = label;

var helloWorld:State = new State();
helloWorld.name =helloWorld;
helloWorld.overrides = [addLabel];

states = [helloWorld];
currentState =helloWorld;
}
]]>
< / fx:Script>

< s:layout>
< s:VerticalLayout />
< / s:layout>


< / s:WindowedApplication>


I wanna add dynamically (at runtime) new states to a container and, to this states, add different elements (like TextInput, Label etc.). This must be done from actionscript, I don't use any mxml file. I can add states and change properties or styles for different elements, but I didn't figured out how to add child elements for different states.

Thank you.

Cristi

解决方案

You'll have to manually create an instance of the State class and create overrides for it. Then add the newly created state to the states array of your component.

Here's a small example air application that shows how to do it:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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[
            import mx.events.FlexEvent;
            import mx.states.AddChild;
            import mx.states.AddItems;
            import mx.states.SetProperty;
            import mx.states.State;

            protected function createState(event:MouseEvent):void
            {
                var label:Label = new Label();
                label.text = "World!";

                var addLabel:AddItems = new AddItems();
                addLabel.relativeTo = foo;
                addLabel.position = "after";
                addLabel.items = label;

                var helloWorld:State = new State();
                helloWorld.name = "helloWorld";
                helloWorld.overrides = [addLabel];

                states = [helloWorld];
                currentState = "helloWorld";
            }
        ]]>
    </fx:Script>

    <s:layout>
        <s:VerticalLayout />
    </s:layout>

    <s:Label id="foo" text="Hello" />

    <s:Button label="Create new state" click="createState(event)" />
</s:WindowedApplication>

这篇关于Flex 4 - 创建状态并在运行时从actionscript添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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