本文介绍了有关在另一个mxml文件中捕获按钮单击事件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是在Flex中发现的一个有趣的问题.

this seems to be an interesting question to be discovered in Flex.

我在A.mxml中注册了一个非常简单的按钮事件侦听器:

I registered a very simple button event listener in A.mxml:

<mx:Script><![CDATA[

import mx.controls.Alert;
public function Handler():void
    {
      Alert.show('click event occured');
    }
]]></mx:Script>


<mx:Button label="{resourceManager.getString('resources', 'button.startLab')}" 
        id="nextStepButton" click="Handler()" />

每次单击按钮都可以正常工作.现在,我想要一些有趣的东西,那就是,我想在另一个mxml文件中捕获此buttonClick事件,比如说B.mxml,然后用B.mxml代替A做一些事情.

It works fine when clicking the button everytime.Now I want to have something interesting,that is,I want to capture this buttonClick Event in another mxml file,say B.mxml and do something in B.mxml instead of A.

我对此有些困惑,希望您能给我一些提示和帮助,非常感谢.

I am bit stuck on this,hope you could give me some hint and help,thanks a lot.

推荐答案

有很多解决此问题的方法.最简单(也是最不面向对象的)是让A意识到B,反之亦然.在这种情况下,您只需添加一个侦听器即可.在B中,您可以说a.nextStepButton.addEventListener(MouseEvent.CLICK,myHandler),或者在A中,您可以做到这一点.nextStepButton.addEventListener(MouseEvent.CLICK,b.myHandler). (当实例化一个组件时,您必须在另一个组件上设置对该组件的引用.)

There are a number of approaches to this problem. The simplest (and least object-oriented) is to have A be aware of B, or vice versa. In that case you can just add a listener. In B you could say a.nextStepButton.addEventListener(MouseEvent.CLICK, myHandler), or in A you could do this.nextStepButton.addEventListener(MouseEvent.CLICK, b.myHandler). (When one component is instantiated, you have to set a reference to it on the other component.)

更好的一个步骤是调度一个冒泡的自定义事件,而其中一个组件仍然知道另一个.在B中:a.addEventListener(CustomNavigationEvent.NEXT_CLICK,myHandler),或在B中:b.addEventListener(CustomNavigationEvent.NEXT_CLICK,myHandler).

One step better would be to dispatch a custom event that bubbles, with one of the components still aware of the other. In B: a.addEventListener(CustomNavigationEvent.NEXT_CLICK, myHandler), or in A: b.addEventListener(CustomNavigationEvent.NEXT_CLICK, myHandler).

进一步讲,您可以让事件冒泡到顶部(SystemManager),然后将侦听器添加到SystemManager.这样,B根本不知道A.在B中:this.systemManager.addEventListener(CustomNavigationEvent.NEXT_CLICK,myHandler).

Taking it further, you could just let the event bubble to the top (the SystemManager) and add your listener to the SystemManager. This way B is not aware of A at all. In B: this.systemManager.addEventListener(CustomNavigationEvent.NEXT_CLICK, myHandler).

更进一步,您可以实现自己的事件广播器版本,它只是第三个对象,任何组件都可以访问该对象,通常将其实现为单例,该组件接受侦听器注册并接受事件调度,然后广播该对象.注册的侦听器事件.

Taking it even further, you can implement your own version of an event broadcaster, which is just a third object that is accessible by any component, usually implemented as a singleton, that takes listener registrations and accepts event dispatches, then broadcasts that event to registered listeners.

希望有帮助.

这是第一种方法的代码:

Here's some code for doing it the first way:

在A.mxml中:

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="onCreationComplete(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            public var b:B;

            private function onCreationComplete(e:FlexEvent):void {
                // Note that you have to have a public onClick handler in B
                this.myButton.addEventListener(MouseEvent.CLICK, b.onClick);
            }
        ]]>
    </fx:Script>
    <s:Button id="myButton"/>
</s:Group>

您需要在声明A和B实例的容器中使A意识到B:

You need to make A aware of B in the container that declares instances of both A and B:

MXML:

<mypackage:A id="aComponent" b="bComponent"/>
<mypackage:B id="bComponent"/>

等效于ActionScript:

ActionScript equivalent:

        var aComponent:A = new A();
        var bComponent:B = new B();
        aComponent.b = bComponent;

这篇关于有关在另一个mxml文件中捕获按钮单击事件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 13:07