【问题标题】:Custom Flex 3 Container自定义 Flex 3 容器
【发布时间】:2012-10-27 23:08:26
【问题描述】:

我想在 Flex 3 中创建一个自定义容器。 我希望这个容器有一个内部容器,它可以是 TabNavigator 或 VBox,具体取决于用户设置的某些标志。一旦页面被渲染,这个标志就不会改变,所以我不需要“动态地”从一个组件移动到另一个组件。

到目前为止,我有这个代码:

public class AccNavigator extends Container {
public var container:Container;

public function AccNavigator() {
    if (GlobalSettings.Vertical) { // This is the said variable
        container = new VBox();
    }
    else {
        container = new TabNavigator();
    }
    container.percentHeight = 100;
    container.percentWidth = 100;
}

override protected function createChildren():void {
    super.createChildren();
    this.addChild(container);
}

override public function addChild(c:DisplayObject):DisplayObject {
    if (c == container) {
        // MessageAlert is the same as an Alert but with custom code
        MessageAlert.show("addChild: Adding Container");
        super.addChild(c);
    } else {
        MessageAlert.show("addChild: " + c.toString());
        container.addChild(c);
    }
    return c;
}

override protected function initializationComplete():void {
    // used for bebugging purposes
    MessageAlert.show("container is visible: " + container.visible.toString());
    MessageAlert.show("this is visible: " + visible.toString());
    MessageAlert.show("container children: " + container.numChildren);
    MessageAlert.show("this children: "+ this.numChildren);
}
}

我在 mxml 中使用这个自定义容器,如下所示:

<AccNavigator>
   <HBox>
      <more things...>
   </HBox>
   <HBox>
      <more things...>
   </HBox>
</AccNavigator>

但是当我运行应用程序时,没有一个组件是可见的。 当初始化完成代码执行时,我看到以下内容:

  • 容器可见:true
  • 这是可见的:真
  • 容器子代:2 个
  • 这个孩子:1

我花了一些时间阅读这个http://www.developmentarc.com/site/sites/default/files/understanding_the_flex_3_lifecycle_v1.0.pdf 了解组件生命周期,但我仍然无法理解我的代码中缺少什么。

有人可以帮我告诉我我错过了什么吗?

谢谢。

【问题讨论】:

    标签: actionscript-3 apache-flex flex3 mxml custom-component


    【解决方案1】:

    问题是我不应该扩展 Container。 因此,类声明如下所示:

    public class AccNavigator extends VBox {
    ....
    }
    

    其他都是正确的。

    【讨论】:

      【解决方案2】:

      从您希望容器基于的容器类型扩展。例如如果你想拥有基于 HBox 的东西,则从 HBox 扩展;如果你想实现基于 VBox 的东西,则从 VBox 扩展。从技术上讲,您可以扩展 Container,但由于 Container 太接近层次结构的“根”,您将不得不以某种方式“重新发明轮子”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-19
        • 2011-03-25
        • 2011-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多