在 Flex 中有几种方法可以实现这一点:
<mx:Script source="yourfile.as" />
您也可以在脚本标签中使用includes="yourfile.as" 声明:
<mx:Script
<![CDATA[
include "yourfile.as";
//Other functions
]]>
</mx:Script>
- 使用Code-Behind 模式定义AS 文件中的代码,该文件扩展了您希望MXML 文件扩展的可视组件。然后,您的 MXML 文件简单地扩展了 AS 文件,并且您可以(通过继承)访问所有代码。它看起来像这样(我不确定这是否适用于扩展
Application 的主 MXML 文件):
AS 文件:
package {
public class MainAppClass {
//Your imports here
public function CreationComplete():void {
}
public function EnterFrame(event:Event):void {
}
}
}
MXML 文件:
<component:MainAppClass xmlns:component="your namespace here"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="600"
height="400"
frameRate="100"
creationComplete="CreationComplete()"
enterFrame="EnterFrame(event)">
</component:MainAppClass>
-
使用框架将您正在寻找的功能作为一种“模型”注入,其中包含您将使用的数据功能。在 Parsley 中它看起来会像这样:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="600"
height="400"
frameRate="100"
creationComplete="model.CreationComplete()"
enterFrame="model.EnterFrame(event)">
<mx:Script>
<![CDATA[
[Inject]
[Bindable]
public var model:YourModelClass;
]]>
</mx:Script>
</mx:Application>
想到的两个可以帮助注入的框架是Mate 或Parsley。
我不确定代码隐藏模式是否适用于主 MXML 文件(它扩展了应用程序),因此如果您遇到问题,可以尝试将主 MXML 文件中的内容分解为单独的组件包含在 Main 中。它可能看起来像这样:
Main.mxml:
<mx:Application blah,blah,blah>
<component:YourComponent />
</mx:Application>
你的组件.mxml:
<component:YourComponentCodeBehind creationComplete="model.creationComplete()"...>
//Whatever MXML content you would have put in the Main file, put in here
</component:YourComponentCodeBehind>
YourComponentCodeBehind.as
package {
class YourComponentCodeBehind {
//Whatever AS content you would have put in the Main .as file, put in here
}
}
根据我从 Flex 架构中收集到的信息,这是设置应用程序的一种非常常见的方式:主 MXML 包含一个“视图”,它是应用程序其余部分的入口点。此视图包含构成应用程序的所有其他视图。
希望这是有道理的:)