网络编程

smartinvoke入门系列10——多窗口的实现与互调

发布制作:admin  发布日期:2011/6/8
SmartInvoke还可以轻松的帮助你实现多窗口的功能。窗口之间可以方便的互调。首先我们点击eclipse工具栏上的图标:

?
打开新建对话框,输入如下内容:


?
?
点击完成后eclipse会在First_项目下新建一个SubWin.mxml文件。如下图:


?
?
我们在SubWin.mxml文件中添加一公共方法供FirstWin调用。内容如下:
??????? ?
public function subWinMethod(bean:BeanTest):void{ Alert.show(bean.name); }
 
?

?

这里的方法必须为public的。
FirstWin.mxml文件中添加打开SubWin窗口的代码,如下:
?
??????????? ??

var subWin:FlashShell=FlashShell.CreateInstance0(); subWin.setAppPath("SubWin"); subWin.setSize(400,400); subWin.setText("SubWin............."); subWin.open();

 

?

?

通过调用subWingetFlashViewer方法获得cn.smartinvoke.gui.FlashViewer类型对象,如果说FlashShell对象的作用是控制窗口,那么FlashViewer对象的作用就是控制窗口加载的swf
通过调用FlashViewer对象上的asyncCallFlexApp,与callFlexApp方法可以调用到加载窗口所在flex application对象上的公共方法。方法说明如下:
asyncCallFlexApp(methodName:String, params:Array = null):void
异步调用当前FlashViewer加载的flex application对象的公共方法方法不会挂起当前执行进程,直接返回
param methodName 调用方法名称 param params 调用方法参数
?
callFlexApp(methodName:String, params:Array = null):Object
同步调用当前FlashViewer加载的flex application对象的公共方法方法会挂起当前执行进程直到java返回
方法提供了不同flex applcation互掉的机制 param methodName 调用方法名称 param params 调用方法参数。
所以通过如下代码就可以访问到SubWin.mxml中的方法:
?
??????????? ?

var viewer:FlashViewer=subWin.getFlashViewer(); var bean:BeanTest=new BeanTest(); bean.name="pz"; viewer.asyncCallFlexApp("subWinMethod",[bean]);

 

?

?

?
但是这段代码不能在subWin.open();的后面调用,因为无法确定subWin所加载的swf是否加载完毕。不用当心,smartinvoke为我们提供了cn.smartinvoke.gui.ShellManager这个类,通过他可以监听到当前应用程序窗口的打开与关闭事件,代码如下:
??????????????????????????? ?
ShellManager.Instance.addListener(function (evt:CShellEvent):void{

                                          var shell:FlashShell=evt.widget;

                                          if(evt.type==CShellEvent.Event_Open){

                                                         var viewer:FlashViewer=shell.getFlashViewer();

                                                         var bean:BeanTest=new BeanTest();

                                                         bean.name="pz";

                                                         viewer.asyncCallFlexApp("subWinMethod",[bean]);

                                                }

                                                if(evt.type==CShellEvent.Event_Closed){

                                                         this.labelInfo.text="窗口"+shell.getShellName()+"关闭了";

                                               }

                               },this);

?