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

浪子不回头ぞ 提交于 2019-12-07 12:54:46

问题


Here's the Class:

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

Here's the 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>

Running this application gets this error:

ArgumentError: Error #1063: Argument count mismatch on fnc::Deck(). Expected 1, got 0. at mx.core::Container/createComponentFromDescriptor()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:3579] at mx.core::Container/createComponentsFromDescriptors()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:3493] at mx.core::Container/createChildren()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2589] at mx.core::UIComponent/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:5370] at mx.core::Container/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2526] at mx.core::Application/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Application.as:846] at Practice/initialize()[C:\Documents and Settings\LocalService\My Documents\Flex Builder 3\Practice\src\Practice.mxml:0] at 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] at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3234] at 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] at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2916]

Adding chipCount="0" to the MXML like this:

<?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>

Gets this compile error:

Severity and Description Path Resource Location Creation Time Id Property 'chipCount' is read-only. Practice/src Practice.mxml line 3 1242656555328 26

How do I specify the initial chip count?


回答1:


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

    }
}



回答2:


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;

    }
}



回答3:


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>


来源:https://stackoverflow.com/questions/878043/how-do-i-populate-required-parameters-in-a-custom-mxml-tag

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