title: How would I include an MXML file inline another MXML file?
body: <p>I would like to include an MXML file in my MXML file in the same way you can include an external file in AS3 using the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#include" rel="nofollow">include</a> directive. Using the include directive brings the code from the external file into the original file at compile time placing it in the same scope. </p>
  
  <p>For example,</p>
  
  <p><strong>Application.mxml:</strong></p>
  
  <pre><code>&lt;Application&gt;
  
      &lt;source="external.mxml"/&gt;
  
  &lt;/Application&gt;
  </code></pre>
  
  <p><strong>External.mxml:</strong></p>
  
  <pre><code>&lt;Styles/&gt;
  
  &lt;Declarations&gt;
      &lt;Object id="test"/&gt;
  &lt;/Declarations&gt;
  </code></pre>
  
  <p>I need to keep this code/mxml/xml in the external file in scope with the original. Do not ask me why I want to do this. </p>
  
  <p>Another example. Here is my current code (simplified) all in 1 mxml file:</p>
  
  <p>...</p>
  
  <pre><code>File1.mxml
  &lt;Button click="clickHandler()"/&gt;
  
  &lt;Script&gt;
  public function clickHandler():void {
  
  }
  &lt;/Script&gt;
  </code></pre>
  
  <p>...</p>
  
  <p>Here is what I want:</p>
  
  <p>...</p>
  
  <pre><code>File1.mxml
  &lt;Group&gt;
  
      &lt;source="File2.mxml"/&gt;
  
      &lt;Button click="clickHandler()"/&gt;
  
  &lt;Group&gt;
  
  
  File2.mxml
  &lt;Script&gt;
  public function clickHandler():void {
      trace(this); // File1.mxml
  }
  &lt;/Script&gt;
  </code></pre>
  
  <p>...</p>
  
  <p>I want to split my code out into a separate file...</p>
  
  <p>~~ <strong>Update</strong> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  </p>
  
  <p>Though NOT what I was asking for using a "code behind" scheme achieves partial credit to breaking the code out of the view. So I create a MXML file, MyGroup.mxml or MyGroup.as and that extends Group that contains the clickHandler code. </p>
  
  <p>The problem with this method is that I am then locked to the class type I'm extending, hardcoding the view. So for example I would have to extend Group if the MXML class I want to split into separate files is a Group. </p>
  
  <p>I've worked on projects where this was done and it is not good. People start setting styles and visual aspects or group / view specific properties in the code behind class and later if or when we need to change it or the layout it we have end up with all these dependencies to the container. It becomes a mess. Plus, using Code Behind you can't reuse it (reuse in the way include styles.as is reused). So this is not a solution but thought I'd mention it. </p>
  
  <p>Here is a code behind example,</p>
  
  <p><strong>MyGroupBehind.mxml:</strong>  </p>
  
  <pre><code>&lt;Group&gt;
  
     &lt;Script&gt;
     public function clickHandler():void {
         trace(this); // File1.mxml
     }
     &lt;/Script&gt;
  
  &lt;/Group&gt;
  </code></pre>
  
  <p><strong>MyGroupAhead.mxml:</strong>  </p>
  
  <pre><code>&lt;MyGroupBehind&gt;
  
      &lt;Button click="clickHandler()"/&gt;
  
  &lt;/MyGroupBehind&gt;
  </code></pre>