문제

Why doesn't the Flex framework's mxml language support a constructor for components or accept constructor arguments for components? It's as far as I know not possible to declare an ActionScript object in mxml if it takes constructor arguments. I'm curious about the reason. Is it a design choice by Adobe or related to how declarative languages work? For example, why not allow:

<myNameSpace:MyComponent constructor="{argArray}"/> 
도움이 되었습니까?

해결책

You can read IMXMLObject help API for more information about your question. They're not telling exactly why an mxml doesn't support constructors, but it says that you must control your mxml component throught its lifecycle events: preinitialize, initialize and creationComplete.

I suppose that's a design decision, considering that an mxml gets translated directly to AS3 code (you can compile your application adding keep-generated-actionscript=true and see what it produces).

다른 팁

Even if a class is defined in MXML, it is possible to implement a constructor via instantiating an instance variable as follows. This will get called before various events like "preinitialize" or "creationComplete" are dispatched.

<myNameSpace:MyComponent>
  <fx:Script>
  <![CDATA[
     private var ignored:* = myInstanceConstructor();

     private function myInstanceConstructor():* {
         // Do something - called once per instance
         return null;
     }
  ]]>
  </fx:Script>
</myNameSpace:MyComponent>

Moreover, class variables can be initialized in a similar way as follows.

<myNameSpace:MyComponent>
  <fx:Script>
  <![CDATA[
     private static var ignored:* = myClassConstructor();

     private static function myClassConstructor():* {
         // Do something - called once per class
         return null;
     }
  ]]>
  </fx:Script>
</myNameSpace:MyComponent>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top