سؤال

وهذا هو مماثل لمشاركتي السابقة. ولكن هذه المرة أريد أن استدعاء دالة موجود على الصفحة الرئيسية 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>

وكيف يمكنني استدعاء الدالة changeText من زر على 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");">

وأنا لا يمكن أن نقول فيها النص الحالي يأتي من ولكن إذا كان هو من القائمة قد تضطر إلى بناء بلدكم الحدث المرن المخصص أو إنشاء متغير مشترك لشطري الوصول إليها. وعادة ما يفضل أولا.

وP.S. الشيء الفوقية الحدث ويمكن أيضا أن يتحقق عن طريق إضافة المستمع الحدث عند اكتمال إنشاء التطبيق. أن تضيفها إلى رئيسية هي:

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