这个其实是最基本的实现之一,唯一不爽的是,flex能够自动识别的通常是格式单一整洁的XML,.NET WebService返回的数据集也是XML的格式,不过太过复杂,flex并不能直接从中识别我们真正想要使用的数据,参照网上查找到的做法,我也另外写了一个方法来把数据整理成XML,再以String的方式返回给flex前端,还好,它能看懂。
    public class WsUtil
    
{
        
//……

        
public static string BuildXmlFromData(DataSet ds, AttributeFieldPair[] pairs)
        
{
            DataTable dt 
= ds.Tables[0];
            XmlDocument xmlDoc 
= new XmlDocument();
            XmlNode root 
= xmlDoc.CreateElement("node");
            XmlAttribute xa 
= xmlDoc.CreateAttribute("label");
            xa.Value 
= "摘要";
            root.Attributes.Append(xa);
            xmlDoc.AppendChild(root);
            
//
            foreach(DataRow dr in dt.Rows)
            
{
                XmlNode node 
= xmlDoc.CreateElement("node");
                BuildNodeAttributes(
ref node, pairs, dr);
                root.AppendChild(node);
            }

            
//
            return xmlDoc.InnerXml;
        }


        
protected static void BuildNodeAttributes(ref XmlNode node, AttributeFieldPair[] pairs, DataRow dr)
        
{
            
foreach(AttributeFieldPair pair in pairs)
            
{
                XmlAttribute xa 
= node.OwnerDocument.CreateAttribute(pair.AttributeName);
                xa.Value 
= dr[pair.FieldName].ToString();
                node.Attributes.Append(xa);
            }

        }

    }


    
public struct AttributeFieldPair
    
{
        
public string AttributeName;
        
public string FieldName;

        
public AttributeFieldPair(string an, string fn)
        
{
            
this.AttributeName = an;
            
this.FieldName = fn;
        }

    }

    public class DefaultService : System.Web.Services.WebService
    
{
        
//……

        [WebMethod]
        
public string GetStruct()
        
{
            WsUtil u 
= new WsUtil();
            DataSet ds 
= u.ExecuteQuery("Spector_GetAllProductGroups"new string[]{}new object[]{});
            AttributeFieldPair[] pairs 
= new AttributeFieldPair[1];
            pairs[
0= new AttributeFieldPair("label""Name");
            
string xml = WsUtil.BuildXmlFromData(ds, pairs);
            
return xml;
        }

    }

flex前端把数据直接绑定到tree上:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html" 
    height
="100%" width="100%" pageTitle="TCost Sample">
    
    
<mx:Style source="CSS/Common.css"></mx:Style>
    
<mx:WebService id="WS" wsdl="http://localhost/TCostWS/DefaultService.asmx?WSDL"
        fault
="mx.controls.Alert.show(event.fault.faultString), 'Error'">
        
<mx:operation name="GetStruct" resultFormat="object" result="BindStruct(event.result.toString())">
        
</mx:operation>
    
</mx:WebService>

    
<mx:Script>
        
<![CDATA[
            import mx.controls.Alert;
            
            function BindStruct(result:String):void
            {
                var doc:XMLDocument = new XMLDocument(result);
                tree1.dataProvider = doc;
                mx.controls.Alert("done");
            }
        
]]>
    
</mx:Script>

    
<mx:VBox width="100%" height="99%" paddingLeft="10" paddingRight="10">
        
<mx:HBox verticalAlign="bottom" width="100%">
            
<mx:Label text="TCost" fontSize="32" paddingLeft="20"/>
            
<mx:Label id="lblUser" text="早上好,Spector" paddingLeft="20"/>
            
<mx:HBox verticalAlign="bottom" width="100%" horizontalAlign="right">
                
<mx:ToggleButtonBar>
                    
<mx:dataProvider>
                        
<mx:Array>
                            
<mx:String>按月度汇总</mx:String>
                            
<mx:String>按季度汇总</mx:String>
                        
</mx:Array>
                    
</mx:dataProvider>
                
</mx:ToggleButtonBar>
            
</mx:HBox>
        
</mx:HBox>
        
<mx:HDividedBox width="100%" height="100%" dividerThickness="2">
            
<mx:Panel width="20%" height="100%" title="用户视角">
                
<mx:Accordion width="100%" height="100%" id="accordion1">
                    
<mx:VBox label="BU/产品族" width="100%" height="100%">
                        
<mx:Tree width="100%" height="100%" enabled="true" id="tree1" dataProvider="{WS.GetStruct()}"
                            labelField
="@label" borderThickness="0"></mx:Tree>
                    
</mx:VBox>
                
</mx:Accordion>
            
</mx:Panel>
            
<mx:TitleWindow height="100%" width="100%" title="数据展现">
                
<mx:Label text="windows"/>
            
</mx:TitleWindow>
        
</mx:HDividedBox>
    
</mx:VBox>
</mx:Application>

具体调用的WebService的方法有点奇怪,不是很理解,不过目前先放一放。
需要注意的是,描述URL使用机器名还是localhost是需要统一的,如果我是使用
http://localhost:8700/……
这个路径的话,那页面里面描述WebService路径的时候也要使用locahost,否则会出现跨域访问的错误。
据说从本机环境迁移到发布环境的话,还是要再次面临跨域访问的问题,不过目前也先放一放。
posted on 2007-12-03 18:04  spector  阅读(523)  评论(0编辑  收藏  举报