为什么将这些对象放入MXML会破坏这个功能?

时间:2012-10-12 16:24:33

标签: actionscript-3 flash flex4 flex-spark

我有两个类,VideoPod和RemotePod,RemotePod继承自VideoPod。没有显示这些类中的所有代码,这里是VideoPod的一部分:

        public function showPanel():void {
            if (!pnl.visible) {
                pnl.visible = true;
                pnl.addElement(removeElement(vg));
            }
        }
                .
                .
                .
<s:Panel id="pnl" width="100%" height="100%" fontWeight="normal" visible="false" />
<s:VGroup id="vg" left="0" resize="onResize()" right="0" top="0" bottom="0">

以及RemotePod的一部分:

        private function onCreationComplete():void {
            m_tmrHeartbeat.addEventListener(TimerEvent.TIMER, checkPulse);

            var arrBtns:Array = new Array(4);
            for (var i:int = 0; i < arrBtns.length; i++) {
                arrBtns[i] = new Button();
                arrBtns[i].width = 28;
                arrBtns[i].height = 25;
                arrBtns[i].top = 10;//-28;
            }

            arrBtns[0].right = 10;
            arrBtns[0].setStyle("icon", Images.PATH + "view-fullscreen-3.png");
            arrBtns[0].addEventListener(MouseEvent.CLICK, maximize);
                .
                .
                .
            for each (var btn:Button in arrBtns) {
                addElement(btn);
            }

            m_lblSize.right = 154;
            m_lblSize.top = 18;//-20;
            m_lblSize.text = FULLSCREEN;
            addElement(m_lblSize);

在RemotePod中为creationComplete事件调用onCreationComplete()。几分钟前,我尝试将RemotePod中的按钮和标签移动到实际的MXML中,但这打破了showPanel()函数。它引发的错误基本上有以下消息:&#34;在该组中找不到vg。&#34; (VideoPod继承自s:Group。)

我不明白。我也开始测试看看vg在运行时的宽度是多少,而且它显然只是停留在0.这是什么晦涩难懂的语言功能导致了这个问题?谢谢!

1 个答案:

答案 0 :(得分:1)

MXML类不会继承其父项的MXML子组件。您应该在类构造函数(如果.as)或初始化侦听器(如果.mxml)中使用纯AS3创建Panel和VGroup。

protected var pnl:Panel;
protected var vg:VGroup;

private function onInitialize():void
{
    pnl = new Panel();
    //set pnl properties such as width, height...
    addComponent(pnl);

    vg = new VGRoup();
    //set vg properties such as width, height...
    addComponent(vg);
}

另一种解决方案是为您的基类使用皮肤。

相关问题