flex 中子窗口通过事件刷新父窗口中的数据

经常听到有人问子窗口操作完成之后,要刷新父窗口的数据怎么办??有人用过parentDocument;
有人使用过在子窗口中new一个父窗口的对象,然后通过这个对象访问父窗口的方法或属性;
。。。。。

第一种,也可以实现。但是第二种就没法理解了,严格来说子窗口是不能new父窗口的实例的。。。。最好的方法是用事件,今天就弄一个事件的例子吧,大牛们可以不看。新童鞋务必看看,有帮助的。。。。附件可直接下载

运行页面如下

代码如下:
总共3个文件,一个名为Parent的application,一个名为Child的titleWindow,还有一个是自定义的一个事件。
Parent.mxml

<?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" minWidth="955" minHeight="600">
        <fx:Script>
                <![CDATA[
                        import mx.collections.ArrayCollection;
                        import mx.managers.PopUpManager;
                        
                        [Bindable]
                        public var ac:ArrayCollection=new ArrayCollection([
                                {name:"周结",sex:"男",age:"23"},
                                {name:"小静",sex:"女",age:"19"},
                                {name:"小二",sex:"男",age:"20"},
                                {name:"大牛",sex:"男",age:"44"}
                        ]); 
                        private var child:Child = new Child();
                        protected function button1_clickHandler(event:MouseEvent):void
                        {
                                PopUpManager.addPopUp(child,this,true);
                                PopUpManager.centerPopUp(child);
                                child.addEventListener(MyEvent.SAVE_OK,saveSuccess);
                        }
                        
                        private function saveSuccess(e:MyEvent):void
                        {
                                ac.addItem(e.data);
                        }
                ]]>
        </fx:Script>

        <fx:Declarations>
                <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        </fx:Declarations>
        
        <mx:AdvancedDataGrid id="adg1" designViewDataType="flat" dataProvider="{ac}" width="455" height="240" x="307" y="79">
                <mx:columns>
                        <mx:AdvancedDataGridColumn headerText="姓名" dataField="name"/>
                        <mx:AdvancedDataGridColumn headerText="年龄" dataField="age"/>
                        <mx:AdvancedDataGridColumn headerText="性别" dataField="sex"/>
                </mx:columns>
        </mx:AdvancedDataGrid>
        <s:Button label="新增一条" click="button1_clickHandler(event)" x="518" y="365"/>
</s:Application>

Child.mxml

<?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" width="400" height="300">
        <s:layout>
                <s:BasicLayout/>
        </s:layout>

        <fx:Script>
                <![CDATA[
                        import mx.managers.PopUpManager;
                        protected function button1_clickHandler(event:MouseEvent):void
                        {
                                var obj:Object = new Object();
                                obj.name = dname.text;
                                obj.age = age.text;
                                obj.sex = sex.text;
                                this.dispatchEvent(new MyEvent(MyEvent.SAVE_OK,obj));
                                PopUpManager.removePopUp(this);
                        }
                ]]>
        </fx:Script>

        <fx:Declarations>
                <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        </fx:Declarations>
        <s:TextInput x="135" y="45" id="dname"/>
        <s:TextInput x="135" y="83" id="age"/>
        <s:TextInput x="135" y="127" id="sex"/>
        <s:Button x="164" y="196" label="保存" click="button1_clickHandler(event)"/>
</s:TitleWindow>

源码下载:http://www.ityangba.com/thread-302-1-1.html

posted on 2011-06-21 23:40  IT氧吧  阅读(1044)  评论(0编辑  收藏  举报