从验证程序调度自定义事件

时间:2011-08-02 14:37:38

标签: flex events validation

我希望对某人来说应该是一个相当简单的问题。基本上我正在尝试创建一个字符串Validator,它通过在创建事件时调度事件来从其控制器请求一种新语言。

以下示例是Flex3组件资源管理器的直接副本 - 唯一的不同之处在于我创建了验证程序作为自定义组件,在创建事件时调度该事件。

问题是我的主要应用程序无法听到发送的事件,我不知道为什么。

以下是示例

MyValidator.as

        package components 
{
    import flash.events.Event;
    import mx.validators.StringValidator;

    /**
     * ...
     * @author Beaker
     */
    public class MyValidator extends StringValidator 
    {

        public function MyValidator() 
        {
            trace(">>MyValidator:MyValidator--")
            var _event:Event = new Event("getLanguage", true)
            dispatchEvent(_event)
        }

    }

}

Main.MXML

    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:comp="components.*" creationComplete="onComplete(event)">
     <mx:Script>
         <![CDATA[
                    import flash.events.Event;
                    import mx.controls.Alert;
                    protected function onComplete(evt:Event):void 
                    {
                        trace(">>Main:onComplete--")
                        systemManager.addEventListener("getLanguage",onTest)
                    }
                    protected function onTest(evt:Event):void 
                    {
                        trace(">>Main:onTest--")
                        Alert.show("getLanguage");
                    }
         ]]>

    </mx:Script>

    <comp:MyValidator source="{fname}" 
        property="text" 
        tooShortError="This string is shorter than the minimum allowed length of 4. " 
        tooLongError="This string is longer than the maximum allowed length of 20." 
        minLength="4" maxLength="20"  
        trigger="{myButton}" triggerEvent="click" 
        valid="Alert.show('Validation Succeeded!');"/>

    <mx:Panel title="StringValidator Example" width="75%" height="75%" 
        paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

            <mx:Form>               
                <mx:FormItem label="Enter a name between 4 and 20 characters: ">
                    <mx:TextInput id="fname" width="100%"/>
                </mx:FormItem>

                <mx:FormItem >
                    <mx:Button id="myButton" label="Validate" />
                </mx:FormItem>
            </mx:Form>  
    </mx:Panel>

</mx:Application>

如果没有正确显示,请原谅格式化!

1 个答案:

答案 0 :(得分:3)

MyValidator不是DisplayObject,因此其事件不会冒泡。您应该将侦听器直接添加到MyValidator实例。

变体#1:

<comp:MyValidator source="{fname}" 
        property="text"
        id="validator"
        tooShortError="This string is shorter than the minimum allowed length of 4. " 
        tooLongError="This string is longer than the maximum allowed length of 20." 
        minLength="4" maxLength="20"  
        trigger="{myButton}" triggerEvent="click" 
        valid="Alert.show('Validation Succeeded!');"/>

validator.addEventListener("getLanguage", onTest);

变体#2:

MyValidator.as 中,在类声明之前添加以下元数据:

[Event(name="getLanguage", type="flash.events.Event")]

和用法:

<comp:MyValidator source="{fname}" 
        property="text"
        getLanguage="onTest(event)"
        tooShortError="This string is shorter than the minimum allowed length of 4. " 
        tooLongError="This string is longer than the maximum allowed length of 20." 
        minLength="4" maxLength="20"  
        trigger="{myButton}" triggerEvent="click" 
        valid="Alert.show('Validation Succeeded!');"/>