18.8.注册服务端数据类型

18.8.1. 问题
我需要在自己的应用程序中注册服务端数据类型,以便从RemoteObject 返回的对象能正确转换为远程类的实例。
18.8.2. 解决办法
使用flash.net.RegisterClass 方法或在类申明中标记类为RemoteClass。
18.8.3. 讨论
在反序列化AMF 数据中的对象为类对象时,该类必须事先在Flash Player 中注册,这样反序列化才能得到正确的数据类型,如下面C#定义的类型:
+展开
-C#
using System;
using System.Collections;
namespace oreilly.cookbook.vo
{
public class RecipeVO {
public string title;
public ArrayList ingredients;
public ArrayList instructions
public RecipeVO(){}
}
}

ActionScript 对应的类型为:
+展开
-ActionScript
package oreilly.cookbook.vo
{
public class RecipeVO
public var ingredients:Array;
public var instructions:Array;
public var title:String;
public function RecipeVO(){}
}

服务将会在C#中创建RecipeVO 对象并返回:
+展开
-C#
using System;
using System.Web;
using oreilly.cookbook.vo;
namespace oreilly.cookbook.service
{
public class RecipeService
{
public RecipeService() { }
public RecipeVO getRecipe() {
RecipeVO rec = new RecipeVO();
rec.title = "Apple Pie";
string[] ingredients = {"flour""sugar""apples""eggs""water"};
rec.ingredients = new ArrayList(ingredients);
string[] instructions = {"instructions are long""baking is hard","maybe I'll just buy it at the store"};
rec.instruction = new ArrayList(instructions);
return rec;
}
}
}

当服务返回时,可以这样访问RecipeVO:
+展开
-XML
<mx:RemoteObject id="recipeServicedestination="fluorinesource="oreilly.cookbook.FlexServiceshowBusyCursor="trueresult="roResult(event)fault="roFault(event)" />
<mx:Script>
<![CDATA[
private function initApp():void {
// we have to register the object for the result to be able to properly cast
// as the RecipeVO
flash.net.registerClassAlias("oreilly.cookbook.vo.RecipeVO", RecipeVO);
}
public function serviceResult(e:ResultEvent):void {
var rec:RecipeVO = (e.result as RecipeVO)
}
public function serviceFault(e:FaultEvent):void {
trace(" Error :: "+(e.message as String));
}

]]>
</mx:Script>

当使用registerClassAlias 方法注册类后,匹配的对象可被转换为RecipeVO 类。

加支付宝好友偷能量挖...


评论(0)网络
阅读(87)喜欢(0)flash/flex/fcs/AIR