【问题标题】:Calling MXML inside ActionScript class在 ActionScript 类中调用 MXML
【发布时间】:2009-07-27 20:31:24
【问题描述】:

如何在我的 ActionScript 类中调用 MXML 组件。

// 文件名.mxml

<mx:Canvas x="181" y="180" width="333">
    <mx:Button styleName="LoginButton" id="loginButton" click="checkLogin();" x="160" y="261"/>
    <mx:TextInput styleName="loginTextInput" id="username" x="160" y="161"/>
    <mx:TextInput styleName="loginTextInput" id="password" displayAsPassword="true" x="160" y="191"/>
</mx:Canvas>

// main.as [类文件]

var obj:filename= new filename();
private function label_link(evt:TextEvent):void 
{
    obj.currentState = "defaultindex";
    obj.username.text = "";
    obj.password.text = "";
}

目标

我需要在我的 ActionScript 类中访问 MXML 的用户名和密码字段。

【问题讨论】:

    标签: apache-flex


    【解决方案1】:

    假设您在文件夹 src/com/stackoverflow/coolstuff/MyCoolBox.MXML 中有一个 MXML 文件

    import com.stackoverflow.coolstuff.MyCoolBox;
    var coolBox:MyCoolBox = new MyCoolBox();
    // Do something
    

    【讨论】:

    • TypeError:错误 #1009:无法访问空对象引用的属性或方法。当我尝试清除文本框时出现此错误...coolBox.TextElementID = "";
    • 您能发布 MXML 类,以便我更好地了解要查找的内容吗?
    • 根据您的要求添加,请帮帮我
    • 组件没有初始化,所以控件全部为空。您需要通过 coolBox.addEventListener 订阅 CREATION_COMPLETE。
    • id 属性被编译为公共变量,但在通过初始化调用 createChildren 之前它们不会被实例化。对于 Container 的子类,根据它的 creationPolicy 进行创建。 adobe.com/livedocs/flex/3/html/layoutperformance_05.html
    【解决方案2】:

    好的,一系列问题:

    1. 另一个标签在哪里?是否有可能以某种方式使用了错误的文件?
    2. 哪条线路有故障?是 obj.password 还是 obj.currentState(范围问题)?
    3. 跟踪时会发生什么(obj);
      1. 跟踪时会发生什么(getQualifiedClassName(obj))?(记得先导入 flash.utils.getQualifiedClassName)
        第二个跟踪应该与 import 语句完全相同。
      2. 当您跟踪 (obj.numChildren) 时,您是否得到了您期望的数字?
      3. 当你跟踪(obj.getChildAt(0))时,你得到一个画布吗?
        1. 当你跟踪(Canvas(obj.getChildAt(0)).getChildAt(0))时,你得到另一个Canvas吗?
        2. 当你追踪时(Object(obj.getChildAt(0)).getChildAt(0).getChildren());
    4. 换个思路——这是一个 MXML Flex 项目还是一个 Actionscript Flex 项目? (我已经看到 MXML 的处理方式发生了变化)。

    这些是我将开始使用的调试命令。如果您得到意外的结果,请在设置状态之前跟踪所有内容。如果您仍然卡住,请告诉我。

    【讨论】:

      【解决方案3】:

      您是否已通过 addChild 将 obj 添加到您的 main.as 中? main.as 的基类是什么?您可以通过在 main.as 构造函数中调用 obj.initialize() 来测试它。

      在调用 initialize 之前不会创建子级,通常在将组件添加到其父级时通过 addChild。但是,您的 main.as 不是 MXML 文件,这意味着它不会自动调用这些方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 2011-03-29
        • 2011-06-05
        • 1970-01-01
        相关资源
        最近更新 更多