flex 应用程序的代码隐藏

Code behind for a flex application

我正在处理 Flex 项目,"connecting" mxml 文件的代码隐藏有问题(它实际上在另一个项目中工作过)。这两个文件都在默认包中。

Hydw.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns="*">

    <s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</s:Application>

Hydw.as:

package  
{
    import flash.events.*;
    import flash.external.*;
    import flash.media.*;

    import mx.controls.TextArea;
    import mx.core.*;
    import mx.events.*;

    import spark.components.*;

    public class Hydw extends spark.components.Application
    {

        public var txt_log:spark.components.TextArea;

        public function Hydw ()
        {
            super();

            addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler);
        }

        private function creationCompleteHandler(param1:FlexEvent) : void
        {
            WriteToLog("creationCompleteHandler");
        }

       public function WriteToLog(s:String) : void 
       {
           txt_log.text += s + "\n";
       }

我 运行 应用程序(发布后),但我在 TextArea 中看不到任何内容。为什么? 顺便说一句,我现在在调试时遇到了麻烦,所以我无法确定到底是哪里出了问题。

显然它没有用。需要对 ActionScript 和 mxml 文件进行一些更改。

首先: 从 ActionScript 文件中删除包和 class,例如:

import mx.events.FlexEvent;

public function creationCompleteHandler(param1:FlexEvent) : void
{
    WriteToLog("creationCompleteHandler");
}

public function WriteToLog(s:String) : void 
{
    txt_log.text += s + "\n";
}

因为它在默认包中,所以不需要定义包和class。

第二个:

从 as 文件中删除 public var txt_log:spark.components.TextArea;。因为会和mxml文件中textArea的id冲突txt_log

第三个:

从 as 文件中删除 addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler); 并在 mxml 文件中提供创建完成事件。喜欢:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script source="Hydw.as" />

        <s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</s:Application>

另一件事是您忘记将 as 文件包含在 mxml 中。喜欢:

<fx:Script source="Hydw.as" />

希望您理解并帮助前进。

这就是你想要的

Hydw.mxml

<?xml version="1.0" encoding="utf-8"?>
<abstract:Hydw 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:abstract="test.pack.abstract.*" 
               minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</abstract:Hydw>

和你的Hydw.as:

package test.pack.abstract
{
    import mx.events.FlexEvent;

    import spark.components.Application;
    import spark.components.TextArea;

    [Bindable]
    public class Hydw extends Application
    {

        public var txt_log:TextArea;

        public function Hydw()
        {
            super();
            addEventListener(FlexEvent.CREATION_COMPLETE, init);
        }

        public function init(evt:FlexEvent):void
        {


        }

    }
}

.mxml 代码中使用的任何可视化组件要在 .as class 中使用 必须在 .as class 中声明为 public 绑定变量,或者简单地声明 .as class 作为 [可绑定]

就这些