这与我以前的帖子类似。但这次我想调用主mxml页面上存在的函数。

这是我的主要mxml页面:

main.mxml

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*">
    <mx:Script>
        <![CDATA[   
        public function changeText(currentText:String):void{

            switch (currentText){
                case "changeText":
                    lblOne.text = "More Text";                  
            }
        }
            ]]>
    </mx:Script>

    <mx:HBox x="137.5" y="10" width="100%" height="100%">
        <ns1:menu id="buttons"> </ns1:menu>
    </mx:HBox>
    <mx:Canvas x="137" y="88" width="408.5" height="200">
        <mx:HBox x="0" y="10" width="388.5" height="190">
            <mx:Panel width="388" height="179" layout="absolute">
                <mx:Label x="10" y="10" text="Some Text" visible="{buttons.showLabel}" id="lblOne"/>
            </mx:Panel>
        </mx:HBox>
    </mx:Canvas>
</mx:Application> 

这是我的包含页面:

menu.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
    <mx:Script>
        <![CDATA[
            [Bindable] public var showLabel:Boolean = true;
        ]]>
    </mx:Script>
    <mx:MenuBar width="380" height="58"></mx:MenuBar>
    <mx:Button x="10" y="10" width="80" label="Show" id="btnOne" click="this.showLabel=true;" />
    <mx:Button x="94" y="10" width="80" label="Hide" id="btnTwo" click="this.showLabel=false;"/>
    <mx:Button x="181" y="10" width="80" label="Run Function" id="btnThree" click="{changeText('changeText')}"/>
</mx:Canvas>

如何从menu.mxml上的按钮调用changeText函数?

有帮助吗?

解决方案

将此添加到菜单:

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

 <mx:Button x="10" y="10" width="80" label="Show" id="btnOne" click="this.showLabel=true;dispatchEvent(new Event("buttonClicked"));"/>

将main更改为:

  <ns1:menu id="buttons" buttonClicked="changeText("Your Text");">

我无法分辨当前文本的来源,但如果是来自菜单,则可能需要构建自己的自定义flex事件或为要访问的两个部分创建公共变量。第一种通常是首选。

P.S。事件元数据事物也可以通过在应用程序创建完成时添加事件侦听器来实现。你会添加到main:

buttons.addEventListener("buttonClicked",changeText("Your Text"));

其他提示

有一种更简单的方法,只需使用parentDocument。

改变这个:

<mx:Button x="181" y="10" width="80" label="Run Function" id="btnThree" click="{changeText('changeText')}"/>

为:

<mx:Button x="181" y="10" width="80" label="Run Function" id="btnThree" click="{parentDocument*.changeText('changeText')}"/>**
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top