简体   繁体   English

Flex:如何使代码远离MXML

[英]Flex: How to keep code away from MXML

Can you recommend articles,books and best practices on designing Flex applications? 您能否推荐有关设计Flex应用程序的文章,书籍和最佳实践? (both AIR and web). (AIR和Web)。

I've read Creating components and enforcing separation of concerns with Flex and Building components by using code behind . 我已经阅读了创建组件并 使用后面的代码 强制分离Flex构建组件 的关注点

Does the application always have to start on the Main MXML? 应用程序是否始终必须从主MXML开始? Can't I instantiate the first view from an ActionScript class? 我不能从ActionScript类中实例化第一个视图吗?

How would you add a handler to the first MXML and give the flow control to it? 如何为第一个MXML添加处理程序并为其提供流控制?

I'm trying to write zero code on my MXML files to keep the view decoupled from code. 我正在尝试在我的MXML文件上编写零代码,以使视图与代码分离。 Is this possible in Flex? 这在Flex中是否可行?

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). 简而言之,您通过创建ActionScript基类(MyClassCode.as),然后创建一个继承自您的代码隐藏类(MyClass.mxml)的MXML文件,将代码与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. 一个缺点是MXML文件中的任何UI元素都需要在代码隐藏类中第二次声明,否则我发现这是一种将代码与UI分离的非常有效的方法。 Here's an example and some links for more info: 这是一个示例和一些链接以获取更多信息:

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

Some links: 一些链接:

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

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

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

Best practices are very subjective in software development. 最佳实践在软件开发中非常主观。 If you find one person who says "X" I can find another who says "Y" and most likely they'd both be right in the given circumstances. 如果你发现一个人说“X”,我可以找到另一个说“Y”的人,而且很有可能他们在特定情况下都是正确的。

Most books I'm aware of focus on bringing beginners up to speed, as opposed to best practices. 我所知道的大多数书都专注于让初学者加快速度,而不是最佳实践。

To answer your specific questions: 回答您的具体问题:

Does the application always have to start on the Main MXML? 应用程序是否始终必须从主MXML开始? Can't I instantiate the first view from an ActionScript class? 我不能从ActionScript类中实例化第一个视图吗?

In theory, it seems like it should be possible to have the main application file be ActionSCript; 理论上,似乎应该可以让主应用程序文件为ActionSCript; after all the Flex compiler just turns MXM into ActionSCript. 毕竟Flex编译器只是将MXM转换为ActionSCript。 In practice, I've never seen anyone do this. 在实践中,我从未见过有人这样做过。 I've seen applications that are all ACtionSCript except for the application tag in the main application file. 我见过除了主应用程序文件中的应用程序标记之外的所有应用程序都是ACtionSCript。

How would you add a handler to the first MXML and give the flow control to it? 如何为第一个MXML添加处理程序并为其提供流控制?

What do you mean by handler and flow control? 处理程序和流量控制是什么意思? I'm not sure I have a specific answer here. 我不确定我在这里有具体的答案。 A lot of people make use of frameworks. 很多人都使用框架。 Cairngorm is the most widely used, but some find it overly complicated. Cairngorm是使用最广泛的,但有些人发现它过于复杂。 For a while Mate was the community favorite. 有一段时间,Mate是社区的最爱。 RobotLegs is the current favorite. RobotLegs是目前的最爱。

I'm trying to write zero code on my MXML files to keep the view decoupled from code. 我正在尝试在我的MXML文件上编写零代码,以使视图与代码分离。 Is this possible in Flex? 这在Flex中是否可行?

It depends. 这取决于。 Isn't the view also code? 是不是视图也是代码? If you want to use a "Model View Controller" Style approach, there are plenty of ways. 如果您想使用“模型视图控制器”样式方法,有很多方法。 Frameworks can help and I mentioned a few above. 框架可以提供帮助,我在上面提到了一些。 But, you could also go it your own. 但是,你也可以自己动手。 If you're new to Flex I'd recommend you start your development "Frameworkless" and the bring frameworks into the equation to see if they help solve problems you run into. 如果您是Flex的新手,我建议您开始开发“Frameworkless”并将框架纳入等式,看看它们是否有助于解决您遇到的问题。

Paul Williams has some great articles and examples on different presentation patterns for Flex. Paul Williams在Flex的不同表示模式上有一些很棒的文章和例子。 He has even built a sample application using each of the different patterns and showed how to unit test some of the patterns. 他甚至使用每种不同的模式构建了一个示例应用程序,并展示了如何对一些模式进行单元测试。 http://blogs.adobe.com/paulw/ http://blogs.adobe.com/paulw/

Take a look at the Passive View model, it may be what you are looking for in terms of writing no AS code in your MXML. 看一下被动视图模型,它可能就是在MXML中没有编写AS代码的方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM