In fact, similar to using JavaScript and other script files on a webpage, there are three main methods.
1. inline modeXML code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <! -- Main. mxml -->
- <? XMLVersion = "1.0" encoding = "UTF-8"?> <Mx: ApplicationXmlns: MX = "http://www.adobe.com/2006/mxml" layout = "vertical">
- <Mx: buttonLabel = "Say hello" Click = "MX. Controls. Alert. Show ('hello, flying ')"/>
- </MX: Application>
<?xml version="1.0" encoding="utf-8"?><!--main.mxml --><?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"><mx:Button label="Say Hello" click="mx.controls.Alert.show('Hello,Flying')"/></mx:Application>
This method directly uses the as method as the attribute value of the event. Of course, this method generally has only one row, which is relatively simple. If you want to pass the logarithm to the method, this method is not desirable.
Note:
Alert. Show (''); single quotation marks are used here. Do not use double quotation marks.
2. Cascade MethodsXML code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
- <! -- Main. mxml -->
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <Mx: ApplicationXmlns: MX = "http://www.adobe.com/2006/mxml" layout = "vertical">
- <Mx: buttonLabel = "Say hello" Click = "sayhello ('flying ')"/>
- <Mx: SCRIPT>
- <! [CDATA [
- Import MX. Controls. Alert;
- Private function sayhello (param_name: string): void {
- Alert. Show ("hello," + param_name );
- }
- ]>
- </MX: SCRIPT>
- </MX: Application>
<!--main.mxml --><?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"><mx:Button label="Say Hello" click="sayHello('Flying')"/><mx:Script><![CDATA[import mx.controls.Alert;private function sayHello(param_name:String):void {Alert.show("Hello, "+param_name);} ]]></mx:Script></mx:Application>
This method puts the as method into the <mx: SCRIPT> </MX: SCRIPT> code block, uses the method as the event property value, and can pass parameters when calling the method, in this way, the as method is reused in a file.
3. External ConnectionXML code {
DP. Sh. toolbar. copytoclipboard (this); Return false;
} "Href =" http://writeblog.csdn.net/# ">
- <! -- Main. mxml -->
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <Mx: ApplicationXmlns: MX = "http://www.adobe.com/2006/mxml" layout = "vertical">
- <Mx: scriptSource = "myfunction."/>
- <Mx: buttonLabel = "Say hello" Click = "sayhello ('flying ');"/>
- </MX: Application>
- // Myfunction. asimport
- MX. Controls. Alert;
- Private function sayhello (param_name: string): void {
- MX. Controls. Alert. Show ("hello," + param_name );
- }
<!--main.mxml --><?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"><mx:Script source="myFunction.as"/> <mx:Button label="Say Hello" click="sayHello('Flying');"/></mx:Application>// myFunction.asimportmx.controls.Alert;private function sayHello(param_name:String):void {mx.controls.Alert.show("Hello, "+param_name);}
The preceding method creates an as file for the AS method, sets this method to the source attribute value of the script element of the called file, and passes parameters when calling the method, this file can be called in multiple files, so that the AS method can be reused in multiple files.