4 Replies Latest reply on Feb 3, 2009 11:22 AM by -Hob

    Creating an MXML component at runtime-is it possible ???

      Hello. I want to create dynamically an object that could be treated as mxml component at runtime. Any idea how to do this ? I was thinking if I create an empty mxml file manually (for example a component that extends Canvas) and then if at runtime I add xml tags defining flex components and if later I instantiate this component would it add the components added at runtime or just the blank Canvas that is initially created ?? Is that possible without creating a file ? Can't I just create a string or XML object with the structure of an MXML and tell the compiler to treat it as an MXML object and convert the tags to AS ? Thanks.
        • 1. Re: Creating an MXML component at runtime-is it possible ???
          -Hob Level 1
          Why would you create a dynamic mxml file rather then just using the built-in actionscript api methods for addChild(), addChildAt(), removeChild(), etc...?

          The problem with creating the mxml file at runtime, is that before you can use it, it must be compiled.
          • 2. Re: Creating an MXML component at runtime-is it possible ???
            levancho Level 3
            MXML is just convenience, that adobe was kind enough to provide, at runtime mxml turns into AS, which further turns into bytecode, so you dont need to create mxml files, if you look into api ref docs most of the AS major components have mxml equivalent, for example HTTPService etc ..
            everything that you do in MXML you can do directly through AS (theoretically at least) :) .if you pass compiler parameter -keep-generated-files (or something like this) after build you will see all generated as that is result of your mxml files.
            so MXML is kind of like a mythical source code :).
            you can create components like Hob, suggested instantiate it and the use addchild()

            • 3. Creating an MXML component at runtime-is it possible ???
              Pavlinius Level 1
              Well I create charts at runtime and I find it much easier to create a MXML file that can be saved to the local disk (I'm building AIR application) and open at later time by the user. At the same time I would like to use the compiler to visualise the chart. Right now I'm building an XML file that describes the chart through the steps and then I have to create methods that build the chart from this XML file. It would be half of the work to just build a MXML file(and class) and then tell the Flex compiler - compile and instantiate this class.

              Hob: I suppose that the MXML file has to be compiled that leads to a question - can you tell the compiler to compile any source at runtime ?
              • 4. Re: Creating an MXML component at runtime-is it possible ???
                -Hob Level 1
                In order to invoke the compiler you would have to be able to run system commands from your app. If this is a Flex app, that's not too hard. You just have whatever server technology your using spawn a system process and invoke the compiler.

                From AIR its more difficult. It has no ability to spawn system processes, so you'd have to use Merapi. Merapi is a java/AIR bridge. Basically, the way it works is, you launch a java process which in turn launches your AIR app. Air can then communicate with java by generating system output.

                Merapi has a set of APIs in both Java and AS3 to facilitate all of this, of course. Once you have it working, you can write java code to invoke the compiler.

                All in all, I'd say it would probably be less work to just write your own domain specific xml variant that you can then parse at run time and create charts from.