TitleWindow中的Flex dispached事件不是系统范围的? [英] Flex dispached event inside TitleWindow is not system wide?
问题描述
我有一棵树,childs dispaching事件,如果我运行TitleWindow之外的对象 - 一切工作正常,但如果我封装在一个TitleWindow内作为源到下面 - 事件不再是系统宽泛。 / p>
<?xml version =1.0encoding =utf-8?>
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx
xmlns:sparkTree =com.sparkTree。*
creationComplete =init() >
< fx:Script>
<![CDATA [
import mx.controls.Alert;
导入mx.managers.PopUpManager;
private function init():void
{
PopUpManager.addPopUp(xWin,this);
this.addEventListener(MyEvent.REQUEST_FORWARD,function(e:MyEvent):void {Alert.show(:P)});
}
]]>
< / fx:Script>
< sparkTree:Tree labelField =label
dataProvider ={dataProviderXML}
width =300height =500
textRollOverColor =yellow
textSelectedColor =0xFFFFFF
itemRenderer =com.sparkTree.XItemRenderer>
< sparkTree:layout>
< / sparkTree:layout>
< / sparkTree:树>
< / s:HGroup>
< / s:TitleWindow>
< / s:Application>
更改您的
this.addEventListener(MyEvent.REQUEST_FORWARD,function(e:MyEvent):void {Alert.show(:P)});
到
xWin.addEventListener(MyEvent.REQUEST_FORWARD,function(e:MyEvent):void {Alert.show(:P)});
或者你正在使用事件冒泡?
我不知道你的代码的细节,看到它很有问题,但是这个代码工作正常:
< xml version =1.0encoding =utf-8?>
xmlns:mx =library:// ns。 adobe.com/flex/mxxmlns:s =library://ns.adobe.com/flex/spark>
< fx:Script>
<![CDATA [
import mx.controls.Alert;
导入mx.managers.PopUpManager;
保护函数init():void
{
PopUpManager.addPopUp(xWin,this);
systemManager.addEventListener(MyEvent.REQUEST_FORWARD,function(e:MyEvent):void
{
Alert.show(:P)
});
}
]]>
< / fx:Script>
< fx:声明>
verticalCenter =0/>
< / s:TitleWindow>
< / fx:声明>
< / s:Application>
其中 MyEvent
是:
包
{
import flash.events.Event;
public class MyEvent extends Event
{
public static const REQUEST_FORWARD:String =requestForward;
$ b public function myEvent(type:String)
{
super(type,true,false);
但是我个人呢推荐改变你的所有代码:)
首先,我强烈建议你把你的窗口放在一个单独的组件中(你应该一直这样做,并使用内联组件仅用于原型设计):
<?xml version =1.0encoding =utf-8?>
xmlns:s =library://ns.adobe.com/flex/spark
xmlns:mx =library://ns.adobe.com/flex/mx>
< fx:元数据>
[Event(name =requestForward,type =MyEvent)]
< / fx:Metadata>
verticalCenter =0/>
< / s:TitleWindow>
其次,不要使用冒泡事件而不需要外部组件:
包
{
import flash.events.Event;
public class MyEvent extends Event
{
public static const REQUEST_FORWARD:String =requestForward;
public function MyEvent(type:String)
{
super(type);
三,尽量不要使用内部函数并创建单独的方法。它更可读,更容易取消订阅。因此,我们的最终申请:
pre $ <?xml version =1.0encoding =utf-8?>
xmlns:mx =library:// ns。 adobe.com/flex/mxxmlns:s =library://ns.adobe.com/flex/spark>
< fx:Script>
<![CDATA [
import mx.controls.Alert;
导入mx.managers.PopUpManager;
保护函数init():void
{
var win:MyWindow = MyWindow(PopUpManager.createPopUp(this,MyWindow));
win.addEventListener(MyEvent.REQUEST_FORWARD,win_requestForwardHandler);
$ b private function win_requestForwardHandler(event:MyEvent):void
{
Alert.show(:P);
var win:MyWindow = MyWindow(event.currentTarget);
win.removeEventListener(MyEvent.REQUEST_FORWARD,win_requestForwardHandler);
}
]]>
< / fx:Script>
< / s:Application>
I have a tree, which childs dispaching event, if i run the object outside TitleWindow - everything is working fine, but if i encapsulate it inside a TitleWindow as into the source below - the event is not anymore dispached system wide.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:sparkTree="com.sparkTree.*"
creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
private function init():void
{
PopUpManager.addPopUp( xWin, this );
this.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );
}
]]>
</fx:Script>
<s:TitleWindow id="xWin" >
<s:HGroup width="100%" height="100%">
<sparkTree:Tree labelField="label"
dataProvider="{dataProviderXML}"
width="300" height="500"
textRollOverColor="yellow"
textSelectedColor="0xFFFFFF"
itemRenderer="com.sparkTree.XItemRenderer">
<sparkTree:layout>
<s:VerticalLayout gap="0" variableRowHeight="true"/>
</sparkTree:layout>
</sparkTree:Tree>
</s:HGroup>
</s:TitleWindow>
</s:Application>
解决方案 Change your
this.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );
to
xWin.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );
Or maybe you're using event bubbling?
I don't know details about your code and see it very problematic but this code works fine:
<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
protected function init():void
{
PopUpManager.addPopUp(xWin, this);
systemManager.addEventListener(MyEvent.REQUEST_FORWARD, function(e:MyEvent):void
{
Alert.show(":P")
});
}
]]>
</fx:Script>
<fx:Declarations>
<s:TitleWindow id="xWin">
<s:Button click="xWin.dispatchEvent(new MyEvent(MyEvent.REQUEST_FORWARD))" horizontalCenter="0"
verticalCenter="0" />
</s:TitleWindow>
</fx:Declarations>
</s:Application>
Where MyEvent
is:
package
{
import flash.events.Event;
public class MyEvent extends Event
{
public static const REQUEST_FORWARD:String = "requestForward";
public function MyEvent(type:String)
{
super(type, true, false);
}
}
}
But what about me personally I recommend to to change all your code :)
First of all I strongly recommend you to extract your window in a separate component (you should do it all the time and use inline components only for prototyping):
<?xml version="1.0" encoding="utf-8"?>
<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">
<fx:Metadata>
[Event(name="requestForward", type="MyEvent")]
</fx:Metadata>
<s:Button click="dispatchEvent(new MyEvent(MyEvent.REQUEST_FORWARD))" horizontalCenter="0"
verticalCenter="0" />
</s:TitleWindow>
Second, don't use bubbling events without need especially outside components:
package
{
import flash.events.Event;
public class MyEvent extends Event
{
public static const REQUEST_FORWARD:String = "requestForward";
public function MyEvent(type:String)
{
super(type);
}
}
}
Third, try not to use inner functions and create separate methods. It is more readable and it is easier to unsubscribe. So our final application:
<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.PopUpManager;
protected function init():void
{
var win:MyWindow = MyWindow(PopUpManager.createPopUp(this, MyWindow));
win.addEventListener(MyEvent.REQUEST_FORWARD, win_requestForwardHandler);
}
private function win_requestForwardHandler(event:MyEvent):void
{
Alert.show(":P");
var win:MyWindow = MyWindow(event.currentTarget);
win.removeEventListener(MyEvent.REQUEST_FORWARD, win_requestForwardHandler);
}
]]>
</fx:Script>
</s:Application>
这篇关于TitleWindow中的Flex dispached事件不是系统范围的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!