Flex2+.NET(FlashRemoting方式数据交互)
Flex2+.NET 让FLEX2和.NET实现数据交互
从FLEX2 beta版就开始关注它,主要原因有以下几点:
1、能直接使用生成的swf文件(即和使用FLASH的SWF一样)
2、编写方式更适合程序员
3、很多控件可以用,特别是chart控件,比FLASH中好用很多
但一直很不爽的是它不能用Remoting方式和.NET交互(基础类型如字符、数组可以),复合类型如DataTable和DataSet不行,到处查也没有相关的资料。用WEBSERVICE不论是速度还是.NET部分代码的编写都是难以接受的。
从Fluorine的资料里了解到它可以支持FLASH和FLEX2的AMF格式,但其网站没有对复合类型如DataTable和DataSet绑定的介绍,经过一天的摸索(其实是多用DEBUG状态看变量的值),成功搞定各种类型的绑定。.NET部分和我上一篇文章里的一样,这里只贴出FLEX部分代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApplication()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.net.Responder;
private var gateway:NetConnection;
public function initApplication():void
{
gateway = new NetConnection();
gateway.connect("http://localhost/ViewFlashRemoting/Default.aspx");
}
public function onFault( fault:Object) : void
{
trace( fault );
}
//返回字符串
private function ReturnString():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnString", new Responder(onResultReturnString, onFault),a_txt.text);
}
private function onResultReturnString(result:Object):void
{
returnString_txt.text = result.toString();
}
//返回数组
private function ReturnArray():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnArray", new Responder(onResultReturnArray, onFault),nums.data);
}
private function onResultReturnArray(result:Object):void
{
returnArray_txt.text = result.toString();
}
//返回DataTable
private function ReturnDataTable():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnDataTable", new Responder(onResultReturnDataTable, onFault),"xx");
}
private function onResultReturnDataTable(result:Object):void
{
//serverInfo.initialData 是Fluorine组件的设定,只有这样取出的值是数组
dg01.dataProvider = result.serverInfo.initialData;
}
//返回DataSet
private function ReturnDataSet():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnDataSet", new Responder(onResultReturnDataSet, onFault));
}
private function onResultReturnDataSet(result:Object):void
{
dg02.dataProvider = result.Table_a.serverInfo.initialData;
dg03.dataProvider = result.Table_c.serverInfo.initialData;
dataset_cbo.dataProvider = result.Table_c.serverInfo.initialData;
//注意此处labelField值为第几列,选列名反而无效
dataset_cbo.labelField = "0";
}
]]>
</mx:Script>
<mx:Canvas x="35" y="28" width="277" height="123" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回字符串" width="112" fontSize="14"/>
<mx:TextInput x="14" y="43" id="a_txt"/>
<mx:TextInput x="14" y="84" width="233" id="returnString_txt" editable="false"/>
<mx:Button x="182" y="43" label="Button" id="a_btn" click="ReturnString()"/>
</mx:Canvas>
<mx:Canvas x="426" y="28" width="277" height="123" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回数组" width="112" fontSize="14"/>
<mx:TextInput x="17" y="82" width="233" id="returnArray_txt"/>
<mx:Button x="185" y="44" label="Button" id="b_btn" click="ReturnArray()"/>
<mx:NumericStepper x="17" y="44" width="150" value="6" minimum="1" maximum="10" stepSize="1" enabled="true" id="nums"/>
</mx:Canvas>
<mx:Canvas x="35" y="269" width="277" height="256" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回DataTable" width="112" fontSize="14"/>
<mx:Button x="191" y="12" label="Button" id="c_btn" click="ReturnDataTable()" />
<mx:DataGrid x="10" y="41" width="246" height="203" id="dg01" >
</mx:DataGrid>
</mx:Canvas>
<mx:Canvas x="332" y="269" width="371" height="256" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回字符串" width="112" fontSize="14"/>
<mx:Button x="130" y="12" label="Button" id="d_btn" click="ReturnDataSet()"/>
<mx:ComboBox x="230" y="12" width="129" id="dataset_cbo" editable="false"></mx:ComboBox>
<mx:DataGrid x="188" y="41" width="171" height="203" id="dg03">
</mx:DataGrid>
<mx:DataGrid x="9" y="41" width="171" height="203" id="dg02">
</mx:DataGrid>
</mx:Canvas>
</mx:Application>
另外我发现在asp.net 2.0中可以使用Fluorine等控件,效果和1.1的一样,但由于vs2005生成的asp.net都不像vs2003那样生成dll文件,所以直接像1.1那样做无效。我的解决方案是装英文版vs2005,打能生成dll文件格式网站的补丁(只有英文版有补丁),然后做的网站就能和1.1一样了。
1、能直接使用生成的swf文件(即和使用FLASH的SWF一样)
2、编写方式更适合程序员
3、很多控件可以用,特别是chart控件,比FLASH中好用很多
但一直很不爽的是它不能用Remoting方式和.NET交互(基础类型如字符、数组可以),复合类型如DataTable和DataSet不行,到处查也没有相关的资料。用WEBSERVICE不论是速度还是.NET部分代码的编写都是难以接受的。
从Fluorine的资料里了解到它可以支持FLASH和FLEX2的AMF格式,但其网站没有对复合类型如DataTable和DataSet绑定的介绍,经过一天的摸索(其实是多用DEBUG状态看变量的值),成功搞定各种类型的绑定。.NET部分和我上一篇文章里的一样,这里只贴出FLEX部分代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApplication()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import flash.net.Responder;
private var gateway:NetConnection;
public function initApplication():void
{
gateway = new NetConnection();
gateway.connect("http://localhost/ViewFlashRemoting/Default.aspx");
}
public function onFault( fault:Object) : void
{
trace( fault );
}
//返回字符串
private function ReturnString():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnString", new Responder(onResultReturnString, onFault),a_txt.text);
}
private function onResultReturnString(result:Object):void
{
returnString_txt.text = result.toString();
}
//返回数组
private function ReturnArray():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnArray", new Responder(onResultReturnArray, onFault),nums.data);
}
private function onResultReturnArray(result:Object):void
{
returnArray_txt.text = result.toString();
}
//返回DataTable
private function ReturnDataTable():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnDataTable", new Responder(onResultReturnDataTable, onFault),"xx");
}
private function onResultReturnDataTable(result:Object):void
{
//serverInfo.initialData 是Fluorine组件的设定,只有这样取出的值是数组
dg01.dataProvider = result.serverInfo.initialData;
}
//返回DataSet
private function ReturnDataSet():void
{
gateway.call( "ViewFlashRemoting.Flash.ReturnDataSet", new Responder(onResultReturnDataSet, onFault));
}
private function onResultReturnDataSet(result:Object):void
{
dg02.dataProvider = result.Table_a.serverInfo.initialData;
dg03.dataProvider = result.Table_c.serverInfo.initialData;
dataset_cbo.dataProvider = result.Table_c.serverInfo.initialData;
//注意此处labelField值为第几列,选列名反而无效
dataset_cbo.labelField = "0";
}
]]>
</mx:Script>
<mx:Canvas x="35" y="28" width="277" height="123" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回字符串" width="112" fontSize="14"/>
<mx:TextInput x="14" y="43" id="a_txt"/>
<mx:TextInput x="14" y="84" width="233" id="returnString_txt" editable="false"/>
<mx:Button x="182" y="43" label="Button" id="a_btn" click="ReturnString()"/>
</mx:Canvas>
<mx:Canvas x="426" y="28" width="277" height="123" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回数组" width="112" fontSize="14"/>
<mx:TextInput x="17" y="82" width="233" id="returnArray_txt"/>
<mx:Button x="185" y="44" label="Button" id="b_btn" click="ReturnArray()"/>
<mx:NumericStepper x="17" y="44" width="150" value="6" minimum="1" maximum="10" stepSize="1" enabled="true" id="nums"/>
</mx:Canvas>
<mx:Canvas x="35" y="269" width="277" height="256" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回DataTable" width="112" fontSize="14"/>
<mx:Button x="191" y="12" label="Button" id="c_btn" click="ReturnDataTable()" />
<mx:DataGrid x="10" y="41" width="246" height="203" id="dg01" >
</mx:DataGrid>
</mx:Canvas>
<mx:Canvas x="332" y="269" width="371" height="256" borderColor="#000000" borderStyle="solid">
<mx:Label x="10" y="10" text="返回字符串" width="112" fontSize="14"/>
<mx:Button x="130" y="12" label="Button" id="d_btn" click="ReturnDataSet()"/>
<mx:ComboBox x="230" y="12" width="129" id="dataset_cbo" editable="false"></mx:ComboBox>
<mx:DataGrid x="188" y="41" width="171" height="203" id="dg03">
</mx:DataGrid>
<mx:DataGrid x="9" y="41" width="171" height="203" id="dg02">
</mx:DataGrid>
</mx:Canvas>
</mx:Application>