简体   繁体   English

如何获得Flex中自定义组件的句柄?

[英]how do I get a handle to a custom component in Flex?

I have a custom login component in Flex that is a simple form that dispatches a custom LoginEvent when a user click the login button: 我在Flex中有一个自定义登录组件,它是一种简单的表单,当用户单击登录按钮时,该组件将调度自定义LoginEvent:


<?xml version="1.0" encoding="utf-8"?>
<mx:Form xmlns:mx="http://www.adobe.com/2006/mxml" defaultButton="{btnLogin}">

    <mx:Metadata>
        [Event(name="login",tpye="events.LoginEvent")]
    </mx:Metadata>

    <mx:Script>

        import events.LoginEvent;

        private function _loginEventTrigger():void {
            var t:LoginEvent = new LoginEvent(
                LoginEvent.LOGIN,
                txtUsername.text,
                txtPassword.text);
            dispatchEvent(t);
        }

    </mx:Script>

    <mx:FormItem label="username:">
        <mx:TextInput id="txtUsername" color="black" />
    </mx:FormItem>
    <mx:FormItem label="password:">
        <mx:TextInput id="txtPassword" displayAsPassword="true" />
    </mx:FormItem>
    <mx:FormItem>
        <mx:Button id="btnLogin" 
            label="login" 
            cornerRadius="0" 
            click="_loginEventTrigger()" />
    </mx:FormItem>
</mx:Form>

I then have a main.mxml file that contains the flex application, I add my component to the application without any problem: 然后,我有一个包含flex应用程序的main.mxml文件,将组件毫无问题地添加到该应用程序中:


<custom:login_form id="cLogin" />

I then try to wire up my event in actionscript: 然后,我尝试在动作脚本中关联事件:


import events.LoginEvent;
cLogin.addEventListener(LoginEvent.LOGIN,_handler);
private function _handler(event:LoginEvent):void {
    mx.controls.Alert.show("logging in...");
}

Everything looks good to me, but when I compile I get an "error of undefined property cLogin...clearly I have my control with the id "cLogin" but I can't seem to get a"handle to it"...what am I doing wrong? 一切对我来说看起来都很不错,但是当我编译时,我收到“未定义属性cLogin的错误...很明显,我可以通过id“ cLogin”来控制我,但似乎无法获得“处理此句柄” ...我究竟做错了什么?

Thanks. 谢谢。

ah! 啊! I figured it out...it was a big oversight on mine...it's just one of those days... 我想通了...这对我来说是一个很大的疏忽...那只是那些日子之一...

I couldn't get the handle on my component because it was not yet created...I fixed this by simply waiting for the component's creationComplete event to fire and then add the event listener. 我无法获得组件上的句柄,因为它尚未创建...我通过等待组件的creationComplete事件触发然后添加事件侦听器来解决此问题。

我相信您也可以这样做:

<custom:login_form id='cLogin' login='_handler' />

You can also do something like this I believe: 我相信您也可以这样做:

 <custom:login_form id='cLogin' login='_handler' /> 

Minor clarification as there seem to be some confusion in the original code. 稍作澄清,因为原始代码似乎有些混乱。

Indeed and the reason for this is that a metadata tag has been used to declare the event that is to be made available that way. 的确,其原因是已使用元数据标签来声明将以这种方式提供的事件。

<mx:Metadata>
    [Event(name="login", type="events.LoginEvent")]
</mx:Metadata>

However, there was no need to add the event metadata when instead of a component "event" property ( login='_handler' ) an event listener was used: 但是,使用事件侦听器代替组件的“事件”属性( login='_handler' )时,无需添加事件元数据:

cLogin.addEventListener(LoginEvent.LOGIN,_handler);
  • addEventListener -> no metadata tag needed addEventListener->不需要元数据标签
  • event property in the component tag -> metadata tag required 组件标签中的事件属性->需要元数据标签

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

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