How to pass parameter to custom component written in ActionScript

I have a custom component written in ActionScript. It has a constructor that expects some arguments.

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

Main.mxml

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

But it shows me a mistake saying

 Error 1136: Incorrect number of arguments. Expected 1. 

How do I pass a parameter in an MXML file to this custom ActionScript component?

+4
source share
2 answers

Like tags, MXML does not support class constructors.

In your ActionScript class, you can enable default parameter initialization:

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

Then we implement the create completion 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> 
+5
source

Well, actually it is possible, but for this you need to change the compiler. I read an article about this, but in Russian, and I did not find a single English. I did not do it myself, but the guys were able to write this code:

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

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

0
source

Source: https://habr.com/ru/post/1383271/


All Articles