[英]How can I define custom 'contentGroups' in a custom Flex 4 component?
[英]Flex - How can I define a function in one component from another?
我是一個完整的Flex / Flash新手,運行Adobe Flash Builder 4 Beta2。我有一個主要組件,它需要能夠調用多個彈出窗口,每個彈出窗口除了一個功能和幾個標簽外幾乎都是相同的。 顯然,我更希望能夠定義此函數並在調用彈出窗口時更改這些標簽,而不是擁有大量使用幾乎相同的代碼的.mxml文件,我只是不知道該怎么做。 我想出了如何更改標簽的方法,但不確定如何重新定義功能。
為了簡單起見,假設我的代碼如下所示:
main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" :s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
protected function init():void
{
var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true);
PopUpManager.centerPopUp(alertWindow);
var popInstance:transmitRoundPop = alertWindow as transmitRoundPop;
popInstance.btnTest.label = "NEW";
}
]]>
</fx:Script>
</mx:Module>
popup.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/halo" width="400" height="300" xmlns:flash="services.flash.*">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
public function test():void
{
Alert.show("ORIGINAL");
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<s:Panel x="10" y="10" width="380" height="280" title="Test" id="pnlTest">
<s:Button x="131" y="104" label="OLD" id="btnTest" click="test()"/>
</s:Panel>
</s:Group>
現在說我想在main.mxml中調用它時在popup.mxml中更改test()
...我該怎么做? 請提供詳細信息...記住我是菜鳥:-)
當然,在發布問題后,這個想法立即浮現在我的腦海。 我以為我會在這里發布解決方案,而不是僅僅刪除問題,以防其他人受到影響。
我只是從popup.mxml中完全取出test()
,並在main.mxml中將init()
修改為如下所示:
protected function init():void
{
var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true);
PopUpManager.centerPopUp(alertWindow);
var popInstance:transmitRoundPop = alertWindow as transmitRoundPop;
popInstance.btnTest.label = "NEW";
popInstance.btnTest.addEventListener("click", function():void { Alert.show("REDEFINED"); });
}
如果我得到您的要求,可以使test()為一個函數變量,並為其提供公共訪問權限,以便其他組件可以對其進行更改。
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
// "test" is now a function variable,
// you change it just like any other variable,
// but you can call it as well, just like before.
public var test:Function;
public function defaultTest():void
{
Alert.show("ORIGINAL");
PopUpManager.removePopUp(this);
}
protected function init():void
{
// Setting a default value for test
// otherwise it would give you an error when calling
// an unassigned function variable.
this.test = defaultTest;
...
}
]]>
</fx:Script>
現在在另一個組件中:
public function mainTest():void
{
...
}
...
myPopup.test = mainTest; // setting the variable, note that there are no parentheses.
myPopup.test(); // running the function, note that there are now parentheses.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.