How do I populate required parameters in a custom MXML tag?

大兔子大兔子 提交于 2019-12-05 23:56:55

In answer to brd6644 comment :

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.
            }
        }

    }
}

You're not able to pass parameters to the constructor of an element when you declare it in MXML. You'll need an empty contructor and then have a property called ChipCount. Your code will also have to be updated to handle ChipCount not being set (or set to 0).

My recommendation would be to change Deck to something like this:

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;

    }
}

I believe that if you're extending a UIComponent you cannot pass arguments to the constructor - you'd have to find another way of setting chip count. I'd suggest listening for the initialize event, and set it then:

<?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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!