从本教程
http://www.brighthub.com/internet/web-development/articles/11010.aspx
我找到了下面的代码.有没有办法打破这个,所以mxml文件只有mxml,脚本标签之间的代码放在一个动作脚本文件中?
我找到了下面的代码.有没有办法打破这个,所以mxml文件只有mxml,脚本标签之间的代码放在一个动作脚本文件中?
谢谢.
-缺口
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="600"
height="400"
frameRate="100"
creationComplete="CreationComplete()"
enterFrame="EnterFrame(event)">
<mx:Script><![CDATA[
public function CreationComplete():void
{
}
public function EnterFrame(event:Event):void
{
}
]]></mx:Script>
</mx:Application>
解决方法
在Flex中有几种实现这一点的方法:
>将AS代码放在.as文件中,并使用“source = yourfile.as”attribute in the Script tag:
< mx:Script source =“yourfile.as”/>
您还可以在Script标签中使用includes =“yourfile.as”声明:
<mx:Script
<![CDATA[
include "yourfile.as";
//Other functions
]]>
</mx:Script>
>使用Code-Behind模式,在AS文件中定义代码,扩展您希望MXML文件扩展的可视化组件.然后,您的MXML文件简单扩展了AS文件,并且(通过继承)访问所有代码.它看起来像这样(我不知道这是否适用于扩展应用程序的主要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>
>使用框架将您要查找的功能注入为包含要使用的数据功能的“模型”类型.在欧芹中看起来像这样:
<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.mxml:
<mx:Application blah,blah,blah>
<component:YourComponent />
</mx:Application>
YourComponent.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包含一个“视图”,它是应用程序其余部分的入口点.此视图包含构成应用程序的所有其他视图.
希望有意义:)