1

我在向容器中添加大量自定义元素时会显示一个“正在加载”对话框。我已将对话框设置为在调用最后添加的元素的 creationCompleteHandler 时消失,但对话框会在所有元素显示在屏幕上之前消失(这会导致非常大的延迟)。

这是我正在做的一个例子:

for (var i:int = 0; i < 100; i++) {
    var elem:MyElement = new MyElement();
    elem.name = "elem" + i;
    container.addElement(elem);

    if (i == 99) {
        elem.creationComplete = function():void {
            PopUpManager.removePopUp(loadingDialog);
        };
    }
}

正如我所说,对话框在所有元素出现在屏幕上之前就消失了。有没有办法判断所有自定义元素何时已添加并且当前显示在屏幕上?

更新: 为了澄清,elem.creationComplete只是一个自定义属性函数,当元素的 creationCompleteHandler 被调用时被调用。

4

2 回答 2

1

这些元素,即使它们已按正确的顺序添加,它们也不是按该顺序创建的:

        private function doStuff():void {
            PopUpManager.addPopUp(myPopup, this);
            for (var i:int = 0; i < 10; i++) {
                var elem:MyElement = new MyElement();
                elem.name = "elem" + i;
                container.addElement(elem);

                elem.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:FlexEvent):void {
                    trace("i'm done " + e.target.name);
                });

                if (i == 9) {
                    elem.addEventListener(FlexEvent.CREATION_COMPLETE, function():void {
                        trace("i'll remove the popup " + elem.name);
                        PopUpManager.removePopUp(myPopup);
                    });
                }
            }
        }

给出:

i'm done elem5
i'm done elem7
i'm done elem0
i'm done elem8
i'm done elem6
i'm done elem3
i'm done elem9
i'll remove the popup elem9
i'm done elem1
i'm done elem4
i'm done elem2

您需要添加一个全局变量来检查所有元素是否已实际创建:

public var created:int = 0;

        private function doStuff():void {
            PopUpManager.addPopUp(myPopup, this);

            for (var i:int = 0; i < 10; i++) {
                var elem:MyElement = new MyElement();
                elem.name = "elem" + i;
                container.addElement(elem);
                created++; // <--- increment with each new element

                elem.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:FlexEvent):void {
                    created--;  // <--- decrement when element is created
                    trace("i'm done ", e.target.name);
                    if (created == 0) {
                        trace("i'll remove it ", e.target.name);
                        PopUpManager.removePopUp(myPopup);
                    }
                });
            }
        }

结果是:

i'm done  elem5
i'm done  elem7
i'm done  elem0
i'm done  elem8
i'm done  elem6
i'm done  elem3
i'm done  elem9
i'm done  elem1
i'm done  elem4
i'm done  elem2
i'll remove it  elem2
于 2012-09-27T18:08:07.860 回答
1

为了解决这个问题,我遵循了 jidma 的回答,除了我监听了 PropertyChanged 事件并contentHeight在容器上的属性更改时递减。仅当容器的高度受到添加元素的影响时才会减少,这似乎有效。

于 2012-09-27T23:27:43.030 回答