如何在自定义MXML标记中填充必需参数?

时间:2009-05-18 14:29:00

标签: flex components

这是班级:

package fnc {
    import mx.containers.Canvas;

    public class Deck extends Canvas {

        protected var _chipCount:int;

        public function Deck(chipCount:int) {
            /* Associate some chips with this deck */
            _chipCount = chipCount;
        }

        public function get chipCount():int {
            return _chipCount;
        }
    }
}

这是MXML:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="fnc.*">
    <ns1:Deck horizontalCenter="0" verticalCenter="0">
    </ns1:Deck>
</mx:Application>

运行此应用程序会出现此错误:

ArgumentError:错误#1063:fnc :: Deck()上的参数计数不匹配。预计1,得0。     在mx.core :: Container / createComponentFromDescriptor()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ Container.as:3579]     在mx.core :: Container / createComponentsFromDescriptors()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ Container.as:3493]     在mx.core :: Container / createChildren()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ Container.as:2589]     在mx.core :: UIComponent / initialize()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ UIComponent.as:5370]     在mx.core :: Container / initialize()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ Container.as:2526]     在mx.core :: Application / initialize()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ core \ Application.as:846]     在Practice / initialize()[C:\ Documents and Settings \ LocalService \ My Documents \ Flex Builder 3 \ Practice \ src \ Practice.mxml:0]     在mx.managers :: SystemManager / http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ managers \ SystemManager.as:2009]     在mx.managers :: SystemManager / initializeTopLevelWindow()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ managers \ SystemManager.as:3234]     在mx.managers :: SystemManager / http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ managers \ SystemManager.as:3064]     在mx.managers :: SystemManager / docFrameListener()[C:\ autobuild \ 3.2.0 \ frameworks \ projects \ framework \ src \ mx \ managers \ SystemManager.as:2916]

chipCount="0"添加到MXML中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="fnc.*">
    <ns1:Deck chipCount="0" horizontalCenter="0" verticalCenter="0">
    </ns1:Deck>
</mx:Application>

获取此编译错误:

严重性和描述路径资源位置创建时间ID 属性'chipCount'是只读的。 Practice / src Practice.mxml第3行1242656555328 26

如何指定初始芯片数?

3 个答案:

答案 0 :(得分:6)

在MXML中声明参数时,您无法将参数传递给元素的构造函数。你需要一个空的构造函数,然后有一个名为ChipCount的属性。您的代码也必须更新以处理ChipCount未设置(或设置为0)。

我的建议是将Deck改成这样的东西:

public class Deck extends Canvas {

    protected var _chipCount:int;

    public function Deck() {
        _chipCount = 0; // Default ChipCount and wait for it to be set.
    }

    public function get chipCount():int {
        return _chipCount;
    }

    public function set chipCount(value:int):int {
        // Add logic here to validate ChipCount before setting.
        _chipCount = value;

    }
}

答案 1 :(得分:2)

回答brd6644评论:

package
{
    import mx.containers.Canvas;

    public class Deck extends Canvas
    {
        protected var _chipCount:int;
        private var chipCountChanged:Boolean;

        public function Deck()
        {
            super();
        }

        public function set chipCount(value:int):void
        {
            if (chipCount != value)
            {
                _chipCount = value;
                chipCountChanged = true;
                invalidateProperties();
                //call invalidateSize() if changing chipCount value may change the size of your component
                //call invalidateDisplayList() if changing chipCount value need a redraw of your component
            }
        }

        public function get chipCount():int
        {
            return _chipCount;
        }

        override protected function commitProperties():void
        {
            super.commitProperties();

            if (chipCountChanged)
            {
                chipCountChanged = false;
                //here update properties that change because of chipCount new value.
            }
        }

    }
}

答案 2 :(得分:1)

我相信如果你扩展一个UIComponent你不能将参数传递给构造函数 - 你必须找到另一种设置芯片数的方法。我建议听一下initialize事件,然后设置它:

<?xml version="1.0" encoding="utf-8"?>
<mx:Script>
   public function setChipCount():void{
     myDeck.chipCount = 0;
   }
</mx:Script>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="fnc.*">
    <ns1:Deck id="myDeck" initalize="setChipCount()" horizontalCenter="0" verticalCenter="0">
    </ns1:Deck>
</mx:Application>