问题
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?
回答1:
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>
回答2:
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/
来源:https://stackoverflow.com/questions/8283934/how-to-pass-parameter-to-custom-component-written-in-actionscript