4

I have a custom component written in ActionScript. It has constructor which is expecting some arguments.

I want to include that custom component in mxml like this,

Main.mxml

...
<custom:CustomActionScriptComponent/>  // Error line ..
..

But, it shows me an error saying

Error 1136: Incorrect number of arguments.  Expected 1.

How to pass parameter in MXML file, to that custom ActionScript component?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • MXML requires that classes have no required constructor parameters, and there is no way to pass them one. By setting some other property, you can dictate behavior in the instance – Exhausted Nov 27 '11 at 14:19

2 Answers2

5

As tags, MXML does not support class constructors.

Per your ActionScript class, you could allow default initialization of the parameter:

    public function CustomActionScriptComponent(parameter:Object=null)
    {
        super();
    }

Then implement a creation complete event handler in your MXML:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               creationComplete="creationCompleteHandler(event)">

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                customActionScriptComponent.parameter = {};
            }
        ]]>
    </fx:Script>

    <custom:CustomActionScriptComponent id="customActionScriptComponent" />

</s:Application>
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
0

Well, actually it is possible, but for this purpose you need to change the compiler. I've read article about this, but it's on russian and I didn't find any english one. I didn't make it by myself, but guys were able to write code like this:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Declarations>
        <Timer xmlns="flash.utils.*" new="1000, 1" />
    </fx:Declarations>
</s:Application>

where new contains constructor arguments.

Anyway, I provide a link to the article for someone who will be interested in this http://habrahabr.ru/blogs/Flash_Platform/128703/

Art
  • 831
  • 3
  • 8
  • 26