【问题标题】:Separating MXML and Actionscript分离 MXML 和 Actionscript
【发布时间】:2011-06-05 16:08:27
【问题描述】:

来自本教程http://www.brighthub.com/internet/web-development/articles/11010.aspx 我找到了下面的代码。有没有办法解决这个问题,所以 mxml 文件只有 mxml,脚本标签之间的代码放在 actionscript 文件中?

谢谢。

-尼克

<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>

【问题讨论】:

    标签: apache-flex actionscript-3


    【解决方案1】:

    在 Flex 中有几种方法可以实现这一点:

    &lt;mx:Script source="yourfile.as" /&gt;

    您也可以在脚本标签中使用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>
      

    想到的两个可以帮助注入的框架是MateParsley


    我不确定代码隐藏模式是否适用于主 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 包含一个“视图”,它是应用程序其余部分的入口点。此视图包含构成应用程序的所有其他视图。

    希望这是有道理的:)

    【讨论】:

    • 我想使用模型背后的代码。我尝试构建 AS 文件,它构建了。但在运行时,我收到错误“TypeError: Error #2023: Class MainAppClass$ must inherit from Sprite to link to the root”。尝试构建 mxml 文件时,出现错误 C:\as\MainAppClass.mxml(8): Error: Could not resolve to a component implementation。 enterFrame="EnterFrame(event)"> 我缺少什么来构建这个?
    • @Nikhil,不错的选择。代码隐藏模型非常可靠,有助于将显示相关的内容与处理和数据分离。至于您看到的错误,我预计它是基于尝试使用带有主 MXML 文件的代码隐藏模型的问题。我会发布一些你可能会尝试的编辑。
    • 我通过使 MainAppClass 扩展应用程序,并将 MainAppClass 放入一个文件夹和一个与该文件夹同名的包中来使其工作。我还必须将 mxml 类重命名为其他名称,否则我会得到对 MainAppClass 错误的模棱两可的引用。感谢您的帮助。
    • @Nikhil,非常棒 - 很高兴你明白了 :)
    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2010-10-30
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多