Flex 一周视频培训系列学习笔记(一)
2009-02-24 13:40 Otis's Technology Space 阅读(623) 评论(0) 编辑 收藏 举报使用Http Service 获取数据
先用asp.net mvc构建数据源.
这个是作了修改的MvcContrib下的XmlResult代码.用来返回Northwind 数据库下的Employee数据.格式为xml
using System.Web.Mvc; using System.Xml.Serialization; using System.Data; namespace MvcContrib.ActionResults { /// <summary> /// Action result that serializes the specified object into XML and outputs it to the response stream. /// <example> /// <![CDATA[ /// public XmlResult AsXml() { /// List<Person> people = _peopleService.GetPeople(); /// return new XmlResult(people); /// } /// ]]> /// </example> /// </summary> public class XmlResult : ActionResult { private object _objectToSerialize; private ObjectType _type; /// <summary> /// Creates a new instance of the XmlResult class. /// </summary> /// <param name="objectToSerialize">The object to serialize to XML.</param> public XmlResult(object objectToSerialize) { _objectToSerialize = objectToSerialize; _type = ObjectType.Object; } public XmlResult(object objectToSerialize, ObjectType type) { _objectToSerialize = objectToSerialize; _type = type; } /// <summary> /// The object to be serialized to XML. /// </summary> public object ObjectToSerialize { get { return _objectToSerialize; } } /// <summary> /// Serialises the object that was passed into the constructor to XML and writes the corresponding XML to the result stream. /// </summary> /// <param name="context">The controller context for the current request.</param> public override void ExecuteResult(ControllerContext context) { if (_objectToSerialize != null) { context.HttpContext.Response.ContentType = "text/xml"; switch (_type) { case ObjectType.DataSet: DataSet ds = _objectToSerialize as DataSet; ds.WriteXml(context.HttpContext.Response.OutputStream); break; case ObjectType.DataTable: DataTable dt = _objectToSerialize as DataTable; dt.WriteXml(context.HttpContext.Response.OutputStream); break; default: var xs = new XmlSerializer(_objectToSerialize.GetType()); xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize); break; } } } public enum ObjectType { DataSet, DataTable, Object } } }
新建controller 并写以下action
public XmlResult GetXmlEmployees() { DataSet ds = Gateways.Northwind.Select(Tables.Employees,Tables.Employees.EmployeeID,Tables.Employees.Address, Tables.Employees.BirthDate,Tables.Employees.City,Tables.Employees.Country,Tables.Employees.EmployeeID, Tables.Employees.FirstName,Tables.Employees.LastName,Tables.Employees.Notes,Tables.Employees.PhotoPath) .SetSelectRange(40, 0, Tables.Employees.EmployeeID) .ToDataSet(); ds.DataSetName = "Employees"; ds.Tables[0].TableName = "Employee"; return new XmlResult(ds, XmlResult.ObjectType.DataSet); }
数据已准备OK
下面写mxml,代码如下:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="employeeService.send()" layout="absolute"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.collections.ArrayCollection; [Bindable] private var employeeData:ArrayCollection; private function resultHandler(event:ResultEvent):void{ // Http Service resultFormat property // Possible formats // object, xml, flashvars, text, e4x, array; employeeData = event.result.Employees.Employee; } // Flex list-based controls: // DataGrid, List, Tree, Menu, TileList and HorizontalList ]]> </mx:Script> <mx:HTTPService id="employeeService" url="http://localhost:1819/Employee/GetXmlEmployees" result="resultHandler(event)" requestTimeout="60"> </mx:HTTPService> <mx:DataGrid dataProvider="{employeeData}" width="100%" height="100%" /> </mx:Application>
Demo完成!!