可以在不使用MXML的情况下使用Flex Framework / Components吗?

时间:2022-11-23 11:22:26

Is it possible to use the Flex Framework and Components, without using MXML? I know ActionScript pretty decently, and don't feel like messing around with some new XML language just to get some simple UI in there. Can anyone provide an example consisting of an .as file which can be compiled (ideally via FlashDevelop, though just telling how to do it with the Flex SDK is ok too) and uses the Flex Framework? For example, just showing a Flex button that pops open an Alert would be perfect.

是否可以在不使用MXML的情况下使用Flex Framework和组件?我非常了解ActionScript,并且不想乱用一些新的XML语言只是为了获得一些简单的UI。任何人都可以提供一个包含.as文件的例子,该文件可以编译(理想情况下通过FlashDevelop,虽然只是告诉如何使用Flex SDK也可以)并使用Flex Framework?例如,只显示弹出打开警报的Flex按钮将是完美的。

If it's not possible, can someone provide a minimal MXML file which will bootstrap a custom AS class which then has access to the Flex SDK?

如果不可能,有人可以提供一个最小的MXML文件,它将引导一个自定义AS类,然后可以访问Flex SDK吗?

4 个解决方案

#1


13  

I did a simple bootstrap similar to Borek (see below). I would love to get rid of the mxml file, but if I don't have it, I don't get any of the standard themes that come with Flex (haloclassic.swc, etc). Does anybody know how to do what Theo suggests and still have the standard themes applied?

我做了一个类似于Borek的简单引导程序(见下文)。我很想摆脱mxml文件,但是如果我没有它,我就不会得到Flex附带的任何标准主题(haloclassic.swc等)。有人知道如何做Theo建议并且仍然应用标准主题吗?

Here's my simplified bootstrapping method:

这是我的简化引导方法:

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<custom:ApplicationClass xmlns:custom="components.*"/>

ApplicationClass.as

package components {
    import mx.core.Application;
    import mx.events.FlexEvent;
    import flash.events.MouseEvent;
    import mx.controls.Alert;
    import mx.controls.Button;

    public class ApplicationClass extends Application {
        public function ApplicationClass () {
            addEventListener (FlexEvent.CREATION_COMPLETE, handleComplete);
        }
        private function handleComplete( event : FlexEvent ) : void {
            var button : Button = new Button();
            button.label = "My favorite button";
            button.styleName="halo"
            button.addEventListener(MouseEvent.CLICK, handleClick);
            addChild( button );
        }
        private function handleClick(e:MouseEvent):void {
            Alert.show("You clicked on the button!", "Clickity");
        }
    }
}

Here are the necessary updates to use it with Flex 4:

以下是与Flex 4一起使用的必要更新:

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<local:MyApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:local="components.*" />

MyApplication.as

package components {
    import flash.events.MouseEvent;
    import mx.controls.Alert;
    import mx.events.FlexEvent;
    import spark.components.Application;
    import spark.components.Button;

    public class MyApplication extends Application {
        public function MyApplication() {
              addEventListener(FlexEvent.CREATION_COMPLETE, creationHandler);
        }
        private function creationHandler(e:FlexEvent):void {
            var button : Button = new Button();
            button.label = "My favorite button";
            button.styleName="halo"
            button.addEventListener(MouseEvent.CLICK, handleClick);
            addElement( button );
        }
        private function handleClick(e:MouseEvent):void {
            Alert.show("You clicked it!", "Clickity!");
        }
    }
}

#2


9  

This is a very simple app that does only the basic bootstrapping in MXML. This is the MXML:

这是一个非常简单的应用程序,它只执行MXML中的基本引导。这是MXML:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
  <mx:Script source="Script.as" />
</mx:Application>

This is the Script.as:

这是Script.as:

import mx.controls.Button;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.core.Application;

private function onCreationComplete() : void {
  var button : Button = new Button();
  button.label = "Click me";
  button.addEventListener(MouseEvent.CLICK, function(e : MouseEvent) : void {
    Alert.show("Clicked");
  });

  Application.application.addChild(button);
}

#3


7  

NOTE: The below answer will not actually work unless you initialize the Flex library first. There is a lot of code involved to do that. See the comments below, or other answers for more details.

注意:除非您首先初始化Flex库,否则以下答案实际上不起作用。这样做涉及很多代码。有关详细信息,请参阅下面的评论或其他答案。


The main class doesn't even have to be in MXML, just create a class that inherits from mx.core.Application (which is what an MXML class with a <mx:Application> root node is compiled as anyway):

主类甚至不必使用MXML,只需创建一个继承自mx.core.Application的类(这是一个带有 根节点的MXML类,无论如何都要编译):

package {

  import mx.core.Application;


  public class MyFancyApplication extends Application {

    // do whatever you want here

  }

}

Also, any ActionScript code compiled with the mxmlc compiler -- or even the Flash CS3 authoring tool -- can use the Flex classes, it's just a matter of making them available in the classpath (refering to the framework SWC when using mxmlc or pointing to a folder containing the source when using either). Unless the document class inherits from mx.core.Application you might run in to some trouble, though, since some things in the framework assume that this is the case.

此外,使用mxmlc编译器(甚至是Flash CS3创作工具)编译的任何ActionScript代码都可以使用Flex类,只需在类路径中使它们可用(在使用mxmlc或指向时指向框架SWC)使用时包含源的文件夹)。除非文档类继承自mx.core.Application,否则您可能会遇到一些麻烦,因为框架中的某些内容假定情况就是如此。

#4


0  

Yes, you just need to include the flex swc in your classpath. You can find flex.swc in the flex sdk in frameoworks/lib/flex.swc

是的,您只需要在类路径中包含flex swc。您可以在frameoworks / lib / flex.swc中的flex sdk中找到flex.swc

edit: One more thing: if you're using Flex Builder you can simply create a new ActionScript project, which will essentially do the same as above.

编辑:还有一件事:如果你使用的是Flex Builder,你可以简单地创建一个新的ActionScript项目,它基本上和上面一样。

#1


13  

I did a simple bootstrap similar to Borek (see below). I would love to get rid of the mxml file, but if I don't have it, I don't get any of the standard themes that come with Flex (haloclassic.swc, etc). Does anybody know how to do what Theo suggests and still have the standard themes applied?

我做了一个类似于Borek的简单引导程序(见下文)。我很想摆脱mxml文件,但是如果我没有它,我就不会得到Flex附带的任何标准主题(haloclassic.swc等)。有人知道如何做Theo建议并且仍然应用标准主题吗?

Here's my simplified bootstrapping method:

这是我的简化引导方法:

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<custom:ApplicationClass xmlns:custom="components.*"/>

ApplicationClass.as

package components {
    import mx.core.Application;
    import mx.events.FlexEvent;
    import flash.events.MouseEvent;
    import mx.controls.Alert;
    import mx.controls.Button;

    public class ApplicationClass extends Application {
        public function ApplicationClass () {
            addEventListener (FlexEvent.CREATION_COMPLETE, handleComplete);
        }
        private function handleComplete( event : FlexEvent ) : void {
            var button : Button = new Button();
            button.label = "My favorite button";
            button.styleName="halo"
            button.addEventListener(MouseEvent.CLICK, handleClick);
            addChild( button );
        }
        private function handleClick(e:MouseEvent):void {
            Alert.show("You clicked on the button!", "Clickity");
        }
    }
}

Here are the necessary updates to use it with Flex 4:

以下是与Flex 4一起使用的必要更新:

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<local:MyApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:local="components.*" />

MyApplication.as

package components {
    import flash.events.MouseEvent;
    import mx.controls.Alert;
    import mx.events.FlexEvent;
    import spark.components.Application;
    import spark.components.Button;

    public class MyApplication extends Application {
        public function MyApplication() {
              addEventListener(FlexEvent.CREATION_COMPLETE, creationHandler);
        }
        private function creationHandler(e:FlexEvent):void {
            var button : Button = new Button();
            button.label = "My favorite button";
            button.styleName="halo"
            button.addEventListener(MouseEvent.CLICK, handleClick);
            addElement( button );
        }
        private function handleClick(e:MouseEvent):void {
            Alert.show("You clicked it!", "Clickity!");
        }
    }
}

#2


9  

This is a very simple app that does only the basic bootstrapping in MXML. This is the MXML:

这是一个非常简单的应用程序,它只执行MXML中的基本引导。这是MXML:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
  <mx:Script source="Script.as" />
</mx:Application>

This is the Script.as:

这是Script.as:

import mx.controls.Button;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.core.Application;

private function onCreationComplete() : void {
  var button : Button = new Button();
  button.label = "Click me";
  button.addEventListener(MouseEvent.CLICK, function(e : MouseEvent) : void {
    Alert.show("Clicked");
  });

  Application.application.addChild(button);
}

#3


7  

NOTE: The below answer will not actually work unless you initialize the Flex library first. There is a lot of code involved to do that. See the comments below, or other answers for more details.

注意:除非您首先初始化Flex库,否则以下答案实际上不起作用。这样做涉及很多代码。有关详细信息,请参阅下面的评论或其他答案。


The main class doesn't even have to be in MXML, just create a class that inherits from mx.core.Application (which is what an MXML class with a <mx:Application> root node is compiled as anyway):

主类甚至不必使用MXML,只需创建一个继承自mx.core.Application的类(这是一个带有 根节点的MXML类,无论如何都要编译):

package {

  import mx.core.Application;


  public class MyFancyApplication extends Application {

    // do whatever you want here

  }

}

Also, any ActionScript code compiled with the mxmlc compiler -- or even the Flash CS3 authoring tool -- can use the Flex classes, it's just a matter of making them available in the classpath (refering to the framework SWC when using mxmlc or pointing to a folder containing the source when using either). Unless the document class inherits from mx.core.Application you might run in to some trouble, though, since some things in the framework assume that this is the case.

此外,使用mxmlc编译器(甚至是Flash CS3创作工具)编译的任何ActionScript代码都可以使用Flex类,只需在类路径中使它们可用(在使用mxmlc或指向时指向框架SWC)使用时包含源的文件夹)。除非文档类继承自mx.core.Application,否则您可能会遇到一些麻烦,因为框架中的某些内容假定情况就是如此。

#4


0  

Yes, you just need to include the flex swc in your classpath. You can find flex.swc in the flex sdk in frameoworks/lib/flex.swc

是的,您只需要在类路径中包含flex swc。您可以在frameoworks / lib / flex.swc中的flex sdk中找到flex.swc

edit: One more thing: if you're using Flex Builder you can simply create a new ActionScript project, which will essentially do the same as above.

编辑:还有一件事:如果你使用的是Flex Builder,你可以简单地创建一个新的ActionScript项目,它基本上和上面一样。