软硬度:如何保持code远离MXML软硬、code、MXML

2023-09-09 21:45:20 作者:流离的双眸

您可以推荐的文章,在设计Flex应用程序的书籍和最佳做法? (既AIR和网)。

Can you recommend articles,books and best practices on designing Flex applications? (both AIR and web).

我读过创建组件以及实现使用Flex 和Building组件使用的背后 code。

应用程序是否总是不得不开始在主MXML?我不能实例化的第一个视图从ActionScript类?

Does the application always have to start on the Main MXML? Can't I instantiate the first view from an ActionScript class?

你会如何添加一个处理程序的第一MXML和给予流量控制呢?

How would you add a handler to the first MXML and give the flow control to it?

我试图写在我的MXML文件零code,以防止code分离视图。这是可能的Flex的?

I'm trying to write zero code on my MXML files to keep the view decoupled from code. Is this possible in Flex?

推荐答案

我已经上使用了code隐藏模式,满足你的许多要求,一些项目的工作。简单地说,你隔离的MXML的code。通过创建ActionScript基类(MyClass的code.as),然后创建一个继承自您的code-隐藏类的MXML文件(MyClass.mxml) 。一个缺点是在MXML文件中的任何UI元素需要申报第二次在code-behind类,否则,我发现这是从用户界面分离code的一种非常有效的方法。下面是一个例子,一些链接的详细信息:

I've worked on a few projects that have used the code-behind pattern, which meets many of your requirements. In a nutshell you isolate the code from the MXML by creating an ActionScript base class (MyClassCode.as) and then creating an MXML file that inherits from your code-behind class (MyClass.mxml). One drawback is that any UI elements in the MXML file need to be declared a second time in your code-behind class, otherwise I've found this to be a very effective method of separating code from UI. Here's an example and some links for more info:

MyClass的code.as:

MyClassCode.as:

package mypackage
{
    import flash.events.MouseEvent;

    import mx.events.FlexEvent;

    import spark.components.Button;
    import spark.components.Group;

    public class MyClassCode extends Group
    {
        public var myButton:Button;

        public function MyClassCode()
        {
            super();
            this.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
        }

        private function onCreationComplete(e:FlexEvent):void {
            this.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
            myButton.addEventListener(MouseEvent.CLICK, onClick);
        }

        private function onClick(e:MouseEvent):void {
            // Do something
        }
    }
}

MyClass.mxml:

MyClass.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mypackage:MyClassCode xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" 
                       xmlns:mypackage="mypackage.*">
    <s:Button id="myButton"/>
</mypackage:MyClassCode>

某些链接:

http://learn.adobe.com/wiki/display/Flex/$c$c+Behind

http://ted.onflash.org/2007/02/$c$c-behind-in-flex-2.php

http://blog.vivisectingmedia.com/2008/04/the-flex-$c$c-behind-pattern/