Flex:从包含的组件调用函数 [英] Flex: Call function from included component

查看:21
本文介绍了Flex:从包含的组件调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我之前的帖子类似.但这次我想调用一个存在于主 mxml 页面上的函数.

This is similar to my previous posting. But this time I want to call a function that exists on the main mxml page.

这是我的主要 mxml 页面:

This is my main mxml page:

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 函数?

How do I call the changeText function from the button on menu.mxml?

推荐答案

将此添加到菜单:

  <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"));"/>

将主要更改为:

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

我不知道当前文本来自哪里,但如果它来自菜单,您可能需要构建自己的自定义 flex 事件或创建一个公共变量以供这两个部分访问.通常首选第一个.

I couldn't tell where current text is coming from but if it is from menu you may have to build your own custom flex event or create a common variable for the two parts to access. The first is usually preferred.

附言事件元数据也可以通过在应用程序创建完成时添加事件监听器来实现.您将添加到 main:

P.S. The event metadata thing could also be achieved by adding the event listener when the creation of the application completes. You would add to main:

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

这篇关于Flex:从包含的组件调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆