一步一步学Silverlight 2系列(25):综合实例之Live Search
概述
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章将从Silverlight 2基础知识、数据与通信、自定义控件、动画、图形图像等几个方面带您快速进入Silverlight 2开发。
本节将综合前面几篇介绍与浏览器交互部分内容,做一个综合示例——Live Search
准备知识
在本示例中,我们将通过调用Live Search API,在Silverlight中动态创建DOM结构,将搜索的结果展现出来。在使用Live Search API之前,需要先去Live Search Developer Center申请一个应用程序ID。ITPUB个人空间B,Ci]y
8h+xhk*P w0
申请完成后应用程序ID大约在10分钟左右生效。关于Live Search API的有关详细信息,请大家参考这里。
编写ASMX
直接调用API,返回的信息可能有很多,为了简单起见,我们对返回的结果做一些处理,编写一个SearchResultItem类:
public classSearchResultItemITPUB个人空间qno7J-?9j
{
[3d5m;kp8VWY0public stringTitle {get;set; }
-vl*f#N(u4KMV0ITPUB个人空间 F"@qd;h]kSl
public stringUrl {get;set; }
0C!O/eh'@2vP |x0
R4R _)BYu)v&h5]_0public stringDescription {get;set; }ITPUB个人空间fdwSt1QZgsP
}
添加对Live Search API的Service引用,地址为:http://soap.search.live.com/webservices.asmx?wsdl。

*u~u9d)Nr[0
在ASMX中对返回的结果进行一些处理,Silverlight程序最后将直接调用ASMX。在调用Live Search时需要指定应用程序ID以及本地化的信息等,查询的参数将在Silverlight程序中调用时传入。
[WebMethod]ITPUB个人空间GD&nS_G
publicSearchResultItem[] DoSearch(stringquery)ITPUB个人空间%V5AM9u:k)v~`
{ITPUB个人空间8kWA:i;j"x
MSNSearchPortTypeClients =newMSNSearchPortTypeClient();
L%}U(}+tyW"so0SearchRequestsearchRequest =newSearchRequest();ITPUB个人空间O9l ODRP#?
intarraySize = 1;ITPUB个人空间*L!I`H0^L8G&p
SourceRequest[] sr =newSourceRequest[arraySize];ITPUB个人空间:l4bfD$c(~"`
ITPUB个人空间Du|6o j:O|
sr[0] =newSourceRequest();ITPUB个人空间fV"U7bDl
sr[0].Source =SourceType.Web;
Mpqu8W1jb0
fS]-B%zE0searchRequest.Query = query;
1UF#U$zci6k }0searchRequest.Requests = sr;ITPUB个人空间ega2T%Nt[
ITPUB个人空间vI1V_4E0UZ1|G
searchRequest.AppID ="C0680205851CCC0E38946DB8FF74156C1C826A86";
p8j:Bw0EGQ1|0searchRequest.CultureInfo ="zh-CN";
d Fw,D&c.Q0SearchResponsesearchResponse;
^o_Un |;Y"}0
5Q/H?6I+o*my0searchResponse = s.Search(searchRequest);
o)Ki#]Z2RYItA0ITPUB个人空间)KC+`"by VQ4v
List<SearchResultItem> lists =newList<SearchResultItem>();ITPUB个人空间k(Q1t&d%B*@K;t j7h
foreach(SourceResponsesourceResponseinsearchResponse.Responses)
R&H7^r1[ V%Z&Y0{
K]AV^&E p0Result[] sourceResults = sourceResponse.Results;
gg7}2ITmE0foreach(ResultsourceResultinsourceResults)
v8B`O]uQ)I0{ITPUB个人空间2ou@*s_6QZm+t
SearchResultItemitem =newSearchResultItem();ITPUB个人空间g2y9dr-@ C%gw
if((sourceResult.Title !=null) && (sourceResult.Title !=String.Empty))
:P`4\.l-v#o,\{0item.Title = sourceResult.Title;ITPUB个人空间-wQz"h.r
7T,t.E3w3q C8n3U/XV0if((sourceResult.Description !=null) && (sourceResult.Description !=String.Empty))
U^*LOB"vCX0item.Description = sourceResult.Description;
K F'[7eh$x/d3\0
Zv,{:y}B1MH0if((sourceResult.Url !=null) && (sourceResult.Url !=String.Empty))ITPUB个人空间+]q|7ftV'k2m
item.Url = sourceResult.Url;
D-k\C,F1Wf!i }0
6o@f0o2T|m [Ek0lists.Add(item);
x~-b,r|!v P0}
9t8S/Y8z qu7J sY0}ITPUB个人空间#A/Cu5a0J5j,N9`
returnlists.ToArray();
%Uxo8]%eC.s YK0}
测试一下我们的服务是否正常:

修改测试页
在测试ASPX中,修改Silverlight插件的样式控制,并添加一个div用来显示搜索的结果:
<divstyle="height:100%;">ITPUB个人空间hnf1lKX9y
<asp:SilverlightID="Xaml1"runat="server"
E'I_!xeH"ML0Source="~/ClientBin/TerryLee.SilverlightGoogleSearch.xap"ITPUB个人空间z)H-c1c B
Version="2.0"Width="857"Height="140" />ITPUB个人空间 Ct f.l f$h)H.B
<divid="result"></div>
cUKX"[7r0</div>
定义几个简单的样式:
<style.type="text/css">
N|(RyNH oM0#resultITPUB个人空间 hq%W3cit:j2]vg sa
{ITPUB个人空间c&f