flex : 포함 된 구성 요소의 호출 기능
-
03-07-2019 - |
문제
이것은 이전 게시물과 유사합니다. 그러나 이번에는 기본 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"));"/>
메인 변경 :
<ns1:menu id="buttons" buttonClicked="changeText("Your Text");">
현재 텍스트가 어디에서 나오는지 알 수 없었지만 메뉴에서 나온 경우 자신의 Custom Flex 이벤트를 작성하거나 두 부분이 액세스 할 수있는 공통 변수를 만들어야 할 수도 있습니다. 첫 번째는 일반적으로 선호됩니다.
추신 : 이벤트 메타 데이터는 응용 프로그램 작성이 완료 될 때 이벤트 리스너를 추가하여 달성 할 수 있습니다. 당신은 메인에 추가합니다 :
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')}"/>**
제휴하지 않습니다 StackOverflow