flex中只打开一个窗口(用单例实现)

先用一个单例记住打开的窗口,写个Mxml或AS文件

<?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/mx">
	
	
	<fx:Script>
		<![CDATA[					
			private static var instance:GlobeConfig;
			public var widgetObj:Object =null;
			
			public static function getInstance():GlobeConfig{
				if(null == instance){
					instance = new GlobeConfig();
					instance.init();
				}
				return instance;
			}			
			private function init():void{
				
			}	
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
</s:Group>

在用弹出窗口的时候,把窗口赋值给单例类的属性


<?xml version="1.0" encoding="utf-8"?>
<module:BaseGroup 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:module="com.flexdms.module.*" 
				  width="100%" height="100%" xmlns:component="com.flexdms.component.*"
				  creationComplete="init()" remove = "closeWindow()" >
	<module:layout>
		<s:BasicLayout/>
	</module:layout>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->		
		<s:RemoteObject id="heasiteRemoteObject" destination="heatSiteService" endpoint="messagebroker/amf">
			<s:method name="searchHeatSite" result="searchHeasiteResultHandler(event)" fault="fault(event)"/>
		</s:RemoteObject>
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[			
			import com.flexdms.manage.GlobeConfig;
			import com.flexdms.manage.GlobeMapOper;
			import com.flexdms.pojo.comm.heat.HeatSite;
			import com.flexdms.util.MapOperatorUtil;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.core.IFlexDisplayObject;
			import mx.managers.PopUpManager;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			
			var window:HeatSiteInfoWindow;  //此为弹出窗口
			var heatSiteWindow:HeatSiteChartWindow;
			
			private function init():void{				
				var heatSite:HeatSite = new HeatSite();
				heasiteRemoteObject.searchHeatSite(heatSite);				
			}
			
			private function searchHeasiteResultHandler(event:ResultEvent):void{
				var swList:ArrayCollection=event.result as ArrayCollection;
				datagrid.dataProvider=swList;
			}	
			
			private function fault(e:FaultEvent):void{
				Alert.show(e.toString(),"操作失败!");
				e.stopImmediatePropagation();
			}		
			
			public function clickRecord():void{
				closeWindow();
				
				window = new HeatSiteInfoWindow();				
				GlobeConfig.getInstance().widgetObj=window;  //<-----注意这里!
				var site:HeatSite = datagrid.selectedItem as HeatSite;
				window.parentObj = this;
				window.site = site;
				window.title = site.sitename;
				PopUpManager.addPopUp(window,this,false);
				PopUpManager.centerPopUp(window);
				window.x=Capabilities.screenResolutionX/3;
				window.y=Capabilities.screenResolutionY/4;			
			}
                        //每次用完的时候,关闭窗口,也可以把下面这一个函数,放到任何一个页面初始化前,关掉其它页面的弹出窗口。
                       //这样就能保证始终只有一个窗口在弹出,而且其它页面可以关闭本页面打开的弹出窗口
                        protected function closeWindow():void
			{
				map.infoWindow.hide();
                                // <-------  下面的一段代码,表示如何关闭弹出窗口,即使把下面的代码放到其它页面,也可以关闭本页面的窗口  ------>
                                if(GlobeConfig.getInstance().widgetObj!=null){
					PopUpManager.removePopUp(GlobeConfig.getInstance().widgetObj as IFlexDisplayObject);
					GlobeConfig.getInstance().widgetObj=null;
				}
			}

		]]>
	</fx:Script>
	<component:TitleGroup width="100%" height="100%" title="站点列表" left="2" >
		<mx:DataGrid id="datagrid" right="0" top="0" left="0" bottom="5">
			<mx:columns>
				<mx:DataGridColumn headerText="名称"  dataField="sitename" width="100" />
				<mx:DataGridColumn headerText="定位" textAlign="center" width="60">
					<mx:itemRenderer>
						<fx:Script> 
							<![CDATA[
								import mx.events.FlexEvent;
								
								[Embed(source='assets/mapicon/marked16.png')]
								private var marked16:Class;
								
								[Embed(source='assets/mapicon/nmarked16.png')]
								private var nmarked16:Class;
								
								public function getImage(data:Object):Class {
									
									if(data.xpoint!=0 && data.xpoint!=null ){
										return marked16;
									}else{
										return nmarked16;
									}
								}
								
							]]> 
						</fx:Script>
						<fx:Component>
							<mx:HBox left="20">
								<fx:Script>
									<![CDATA[
										import com.flexdms.consts.IconConst;
									]]>
								</fx:Script>
								<mx:Button id="invisible" visible="false" width="10"	/>
								<mx:Button  toolTip="查看" icon="{IconConst.SEARCH20}" width="20"		
											skin="{null}"
											useHandCursor="true" 
											buttonMode="true"  
											mouseChildren="false"
											click="outerDocument.clickRecord()" />
								
							</mx:HBox>
						</fx:Component>
					</mx:itemRenderer>
				</mx:DataGridColumn>
			</mx:columns>
		</mx:DataGrid>
	</component:TitleGroup>
</module:BaseGroup>









你可能感兴趣的:(单例,Flex,PopUpManager)