the blog
Back to -Blog

ActionScript ’Code Behind’ in Amethyst

Making Flex more at home in Visual Studio
by Dermot Hogan
Thursday 26 March 2009.

The ’mx:Script’ tag in MXML has two forms. Often, you will have code like this

        <mx:Script>
                <![CDATA[
...
                ]]>
        </mx:Script>

That is, the ActionScript code is embedded in the MXML code. This is fine for small pieces of code, but it can be unmanageable for large or more complicated ActionScript.

The alternative approach is to use a link or reference to an ActionScript file with the code in it:

<mx:Script source="\\amethyst\\main\\AmethystMain.as"/>

The problem from Visual Studio’s point of view is that there is nothing to indicate that the ActionScript file is connected to the MXML file. Even worse, the IntelliSense system has no way of knowing that AmethystMain.as is really logically part of another file and also that a control declared in the MXML part should be displayed in an ActionScript completion list .

In the latest release of Amethyst, we’ve solved these two problems by using a technique called ’code behind’. This is usually applied to, for example, the C# form designer where the form has a partial class with the C# control declarations in it. Normally, in C# you don’t see this file – though it is visible in the Solution Explorer, if you look.

The effect in the Solution Explorer is to make the ‘dependent’ file node appear underneath the ‘master’ node like this:

To make an ActionScript file a ‘code behind’ file in Amethyst all you need to do is set the name of the file in the Script tag and refresh the Solution Explorer. Conversely, if you want a file to become independent, then just remove the Script tag.

Here’s some MXML with some embedded code (CDATA) and two ‘code behind’ Script tags:

and here’s what it looks like in the Solution Explorer:

Deleting test2.as will cause it to revert to being a ‘normal’ node:

as you can see here:

The advantages of this approach are first, that the relationship between the Script code and the MXML file is absolutely clear – you don’t have to go hunting for your ActionScript code file – just click on the node in the Solution Explorer and it will open, Secondly, variables declared in the MXML part such as a TextArea control, are available to IntelliSense in the ActionScript part. Thirdly, variables declared in one ActionScript Script tag segment are also available in any other ActionScript tag section which are dependent on a single ‘master’ MXML file.

Bookmark and Share   Keywords:  Amethyst  development
© SapphireSteel Software 2014