0

我在 Flex 4/AS3 中编写了以下代码,但它没有按预期工作。

所以,我想知道我的代码是否有问题......我会解释一下:

TileTest.mxml

<s:Application minWidth="955" minHeight="600" creationComplete="createButtons()"
<fx:Script>
    <![CDATA[
        import Object.TestButton;
        import mx.collections.ArrayList;

        private var _names:Array = ["one", "two", "three"]; 

        public function createButtons():void
        {
            var buttons:ArrayList = new ArrayList();

            for(var a:int = 0; a < _names.length; a++)
            {
                var testButton:TestButton = new TestButton();
                    testButton.customName = _names[a];

                buttons.addItem(testButton);
            }

            myList.dataProvider = buttons;
        }
    ]]>
</fx:Script>
<s:VGroup gap="12" width="100%">
    <s:Label text="Options" fontSize="18" fontWeight="bold" color="#333333" />
    <s:List width="100%" height="100%" id="myList" itemRenderer="Object.TestButton" borderVisible="false">
        <s:layout>
            <s:TileLayout orientation="rows" columnWidth="290" rowHeight="90" columnAlign="left" horizontalGap="0" verticalGap="0" />
        </s:layout>
    </s:List>
</s:VGroup>
</s:Application>

这是我的应用程序。在这里,我有一个名为myList的 List ,我在其中加载了一些TestButton。我为循环内的每个按钮设置了一个名称。

测试按钮

<s:Group width="300" height="90" click="{ Alert.show(_name); }"
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;

        private var _name:String = "just a test...";

        public function set customName(newName:String):void
        {
            _name = newName;

            Alert.show(_name);

            this.addEventListener(MouseEvent.MOUSE_OVER, function():void{ Alert.show(_name); });
        }
    ]]>
</fx:Script>
<s:BorderContainer accentColor="#000000" width="100%" height="100%" />
</s:Group>

正如你所看到的,我在这个组件中有三个警报......我这样做是为了让我们能够理解这个问题。

  • 第一个警报发生在我设置 customName 时,它​​出现在应用程序中,如已显示的那样。

  • 第二个应该发生在 Mouse_Over 上,因为事件侦听器已添加到 Group 元素中。

  • 第三个警报应该发生在 Group 元素中的 Click 上。

现在,如果我运行生成的 swf,我会在屏幕上看到所有三个按钮和三个警报,一个对应于应用程序设置的每个 customName,它会提醒正确的名称。

如果我将鼠标放在任何按钮上,它不会提醒任何消息。

如果我单击任何按钮,它会提醒设置为_name属性的默认消息,即“只是一个测试......”

我不明白是什么问题,因为我希望它总是提醒应用程序为每个按钮设置的名称。另外,我不想更改组件......我的意思是我想要一个 List 和 TestButtons,里面有一个私有字符串。

如果问题出在这些特定的实现中,那么我别无他法,只能改变它......

谢谢你们!

4

1 回答 1

1

问题是TestButton您的数据提供者中有一个 s 列表,而不仅仅是普通数据。这些按钮被创建,这就是您看到正确警报消息的原因。

因此,Flex 会看到一个包含三个项目的列表,因此它会创建三个附加项TestButtons来呈现列表中的数据。但这些TestButtons都是全新的,其属性具有默认值。

要解决这个问题,最好只在数据提供者 ArrayList 中有数据,并且可以通过data项目渲染器的属性访问它。

于 2013-10-08T20:12:22.647 回答