在flex 4.0中,此代码有效:
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="500" height="600">
<fx:Script>
<![CDATA[
import componentCanvas;
import mx.containers.TitleWindow;
import mx.controls.Alert;
public function createChild():void{
var c:componentCanvas = new componentCanvas;
c.x = 20;
c.y=20;
toInclude.addChild(c);
}
]]>
</fx:Script>
<mx:Button click="createChild()"/>
<mx:Canvas id="toInclude"/>
- componentCanvas -
<mx:Canvas xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Script>
<![CDATA[
import componentCanvas;
import mx.containers.TitleWindow;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import popAll;
public function oh():void{
Alert.show("From titleWindow");
}
public function open():void{
var pop:popAll = popAll(PopUpManager.createPopUp(this, popAll, true));
}
]]>
</fx:Script>
<mx:Label text="Canvas" x="100" y="100"/>
<mx:Button click="open()"/>
- popAll -
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" x="40" y="40" close="closePopUp()">
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
public function closePopUp():void{
super.parentDocument.oh();
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<mx:Button click="closePopUp()"/>
当我在flex 4中调用titleWindow中的parentDocument时,一切都很好。 4.5中的相同代码不起作用。
有没有办法在Flex 4.5中执行此操作?
答案 0 :(得分:1)
在所有文件中调用parentDocument
并使用public
函数绝对不是最佳做法!相反,您应该真正研究Flex中的Event Life Cycle以及如何使用它。在我看来,当你想要将组件的特定功能暴露给它的用户时,应该真正创建公共方法。
基本上,您应该从popAll
类派遣一个事件,并在componentCanvas
中创建的实例中监听它。因此,要解决此问题,您的代码应为:
popAll:
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" x="40" y="40" close="closePopUp()">
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
public function closePopUp():void{
this.dispatchEvent(new Event("closePopup"));
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<mx:Button click="closePopUp()"/>
</s:TitleWindow>
和componentCanvas:
<mx:Canvas xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
private var pop:popAll;
public function oh():void{
Alert.show("From titleWindow");
}
public function open():void{
pop = popAll(PopUpManager.createPopUp(this, popAll, true));
pop.addEventListener("closePopup", onClosePopupRequested);
}
protected function onClosePopupRequested(event:Event):void
{
pop.removeEventListener("closePopup", onClosePopupRequested);
oh();
}
]]>
</fx:Script>
<mx:Label text="Canvas" x="100" y="100"/>
<mx:Button click="open()"/>
</mx:Canvas>
根据上面的代码,我从popAll类派遣一个名为closePopup
的新事件,并在创建它的实例的地方收听它。然后,一旦事件被捕获,我将删除事件处理程序,然后从事件处理程序中调用您的oh()
方法。
我会就你的代码提出更多建议:
spark
组件而不是mx
组件。根据我的经验,它们确实具有更多功能。希望这会有所帮助。欢呼声。