Flex include and import ActionScript code _flex

Source: Internet
Author: User
Tags cdata xmlns
Include and import are very different in ActionScript. Include copying code from one file to another, copying the code to the location where the include statement resides. Import adds a reference to a class or package so that you can access the objects and properties defined in other classes. The code files imported through import must exist on the path you imported. Import code files by include, the file address must be entered with the main file can be found on the relative path, or through the absolute path can also be.
You can add ActionScript code using include statements and < Mx:script source= "filename" > tags in your flex application.
You can use the import statement within <mx:Script> to refer to the classes and packages you might need in your flex application.
You can introduce ActionScript code within the <mx:Script> tag by referencing an external Actionscrit file. At compile time, the compiler copies the contents of the entire code file into your Mxml program, just as you actually wrote the code inside the tag. In the <mx:Script> code block, ActionScript statements can only be inside a function. The introduction of code files can also define static variables and namespaces, introduce additional ActionScript files, import declaration definitions, and use namespaces. But you can't define a class for the file you're importing.
The variables and functions defined in the ActionScript file introduced can be allowed to be used by any component in the Mxml file. The name of the ActionScript file introduced cannot be the same as the name of the imported ActionScript class. Flex can access variables and functions in imported files, but cannot add new classes (I understand that the file being imported cannot be a class) because the Mxml file itself is a class.
Imported as files are not necessarily in the same directory as the Mxml file. However, you must allow your program to correctly identify your ActionScript code files in the directory structure.
Here are two ways to introduce an external ActionScript file in a Flex application:
§ Use the Source property of the <mx:Script> label. This is the preferred method for introducing external ActionScript code files.
§ Use include statements in <mx:Script> code blocks.
Using the Source property to introduce ActionScript files
You can use the <mx:Script> tag's source attribute in a flex application to introduce an external ActionScript file. This way you can make the code in your Mxml file less messy and improve the reusability of your code.
Do not make the code file the same as the name of the program file. This will cause the compiler to make an error.
The following is the contents of the Includedfile.as file:
Copy Code code as follows:

Usingas/includes/includedfile.as
Public Function Computesum (A:number, B:number): number {
return a + B;
}

The following example introduces the contents of the Includedfile.as file. The document was made under the subdirectory of the document in which it was introduced.
Copy Code code as follows:

<?xml version= "1.0"?>
<!--Usingas/sourceinclude.mxml-->
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" layout= "Absolute" >
<mx:script source= "includes/includedfile.as"/>
<mx:textinput id= "ta1st" text= "3" width= "x=" "170" y= "a" textalign= "right"/>
<mx:textinput id= "ta2nd" text= "3" width= "x=" "170" y= "" textalign= "right"/>
<mx:textarea id= "Tamain" height= "width=" x= "132" y= "to" textalign= "right"/>
<mx:button id= "B1" label= "Compute Sum"
click= "Tamain.text=string (computesum (number (Ta1st.text), number (ta2nd.text));"
x= "105"
y= "115"
/>
<mx:label x= "148" y= "text=" + "fontweight=" bold "fontsize=" "width=" ""/>
</mx:Application>

The source property of the <mx:Script> label supports both relative and absolute paths.
You cannot use both the source attribute and the ActionScript code within the same <mx:Script> tag. If you need to do this, you need to use two <mx:Script> tags.
Include instruction a ActionScript statement that copies the contents of the specified file to the Mxml file. The syntax for the include directive looks like this:
Include "file_name";
The myfuncitons.as file is introduced in the following example:
Copy Code code as follows:

<?xml version= "1.0"?>
<!--Usingas/includeasfile.mxml-->
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" >
<mx:script><! [cdata[
/* myfunctions.as file defines two functions that return a string.
Include "includes/myfunctions.as";
]]></mx:script>
<mx:button id= "MyButton"
Label= "Call Methods in Included File"
Click= "ta1.text=dosomething (); Ta1.text+=dosomethingelse ()"
/>
<mx:textarea width= "268" id= "Ta1"/>
<mx:button label= "Clear" click= "ta1.text=" "/>
</mx:Application>

You can have each include directive point to a single file, or you can use any number of include directives. Include directives can be nested. Files introduced through the include directive can also be introduced into other files by using the include directive.
The include directive only supports relative paths.
Include directives can only be used where multiple statements are allowed to execute. For example, the following example is wrong:
if (expr)
Include "foo.as"; The first statement after the IF statement can be executed and the remaining statements are not
...
The following example is correct:
if (expr) {
Include "foo.as"; All statements in {} After if can be executed
}
More than one statement is allowed in curly braces so you can add more than one statement to the curly braces.
Adobe recommends: Try not to use the include directive to refer to a large number of ActionScript files. You should divide the code into different classes of files, and then store the class files in the appropriate logical package structure.
The source property of the <mx:Script> label differs from the way the include directive references the file.
The following is a valid path for the Source property of the <mx:Script> tag when referencing an external file:
§ Relative URLs, for example. /myscript.as. The relative path of the file is not determined by a slash opening URL. If <mx:script source= ". /includedfile.as "> is included in" Mysite/myfiles/myapp.mxml, "the system will look for" mysite/includedfile.as ".
The include directive in ActionScript can only refer to relative URLs. Flex looks for imported classes and packages from the source code path. Flex does not look for files that are referenced by the Source property of the file or <mx:Script> tag that contains the include directive from the sources code path.
Importing classes and Packages
If you create multiple common classes or use ActionScript files that store commonly used functions, you might want to store them in a set of classes within a package. You can use the import statement to import Acionscript classes and packages. After importing, you do not have to enter the full class name when you use ActionScript to access the class.
The following example imports the MyClass class in the Mypackage.util package:
Copy Code code as follows:

<?xml version= "1.0"?>
<!--Usingas/accessingpackagedclasses.mxml-->
<mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" >
<mx:script><! [cdata[
Import MyPackage.Util.MyClass;
private var mc:myclass = new MyClass;
]]></mx:script>
<mx:button id= "MyButton" label= "click Me" click= "mybutton.label=mc.returnastring ()"/>
</mx:Application>

In this code, instead of using the full class name (MyPackage.Util.MyClass) to refer to a class, you can use MyClass to refer to a class.
You can also use the wildcard character (*) to import the entire package. For example, the following statement imports the entire Mypackage.util package:
Import mypackage.util.*;
Flex searches for imported files and packages and imports only those classes and packages that are used by the resulting SWF file.
It is not enough to simply define the full class name. You should use the full class name only when you need to distinguish between two classes that have the same name in a different package.
If you import a class in a program but do not use it, then the class will not be compiled into the byte code of the resulting SWF file. Therefore, using the wildcard character (*) to import an entire package does not increase the size of the SWF file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.