【问题标题】:Why does putting these objects into the MXML break this function?为什么将这些对象放入 MXML 会破坏此功能?
【发布时间】:2023-03-03 07:00:23
【问题描述】:

我有两个类,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);

其中 onCreationComplete() 是为 RemotePod 中的 creationComplete 事件调用的。几分钟前,我尝试将 RemotePod 中的按钮和标签移动到实际的 MXML 中,但这破坏了 showPanel() 函数。它引发的错误基本上有以下消息:“vg is not found in this Group。” (VideoPod 继承自 s:Group。)

我不明白。我还开始测试运行时 vg 的宽度是多少,显然它只是保持在 0。导致这种情况的晦涩语言功能是什么?谢谢!

【问题讨论】:

  • 我不认为 RemotePod 可以拥有 MXML,除非您将 VideoPod 编写为模板组件。因此,重要的是查看两者的代码,看看这是否可行。另外,您不应该在 creationComplet 中添加子项,而是在 createChildren 覆盖中添加子项。

标签: actionscript-3 flash flex4 flex-spark


【解决方案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);
}

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

【讨论】:

  • 因此我什至不能重写一个函数,只能通过调用 super.()? 访问父组件
  • 如果函数被声明为受保护或公共,您可以覆盖它。
  • 这就是我试图做的。但是,在其中调用 super.showPanel() 会导致问题。
  • 如果您在 parent.showPanel 中调用 mxml 组件,这是正常的。父类中不能有mxml子组件,一旦扩展就会被清除。
猜你喜欢
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 2021-08-03
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多