(25):Silverlight 2 综合实例之Live Search
概述
准备知识
![](http://img1.51cto.com/attachment/200803/200803251206415780593.png)
编写ASMX
public class SearchResultItem {public string Title { get; set; }public string Url { get; set; }public string Description { get; set; } }
![](http://img1.51cto.com/attachment/200803/200803251206415793046.png)
[WebMethod] public SearchResultItem[] DoSearch(string query) {MSNSearchPortTypeClient s = new MSNSearchPortTypeClient();SearchRequest searchRequest = new SearchRequest();int arraySize = 1;SourceRequest[] sr = new SourceRequest[arraySize];sr[0] = new SourceRequest();sr[0].Source = SourceType.Web;searchRequest.Query = query;searchRequest.Requests = sr;searchRequest.AppID = "C0680205851CCC0E38946DB8FF74156C1C826A86";searchRequest.CultureInfo = "zh-CN";SearchResponse searchResponse;searchResponse = s.Search(searchRequest);List<SearchResultItem> lists = new List<SearchResultItem>();foreach (SourceResponse sourceResponse in searchResponse.Responses){Result[] sourceResults = sourceResponse.Results;foreach (Result sourceResult in sourceResults){SearchResultItem item = new SearchResultItem();if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))item.Title = sourceResult.Title;if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))item.Description = sourceResult.Description;if ((sourceResult.Url != null) && (sourceResult.Url != String.Empty))item.Url = sourceResult.Url;lists.Add(item);}}return lists.ToArray(); }
![](http://img1.51cto.com/attachment/200803/200803251206415807515.png)
修改测试页
<div style="height:100%;"><asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TerryLee.SilverlightGoogleSearch.xap"Version="2.0" Width="857" Height="140" /><div id="result"></div> </div>
<style type="text/css">#result{margin-left:20px;}.urlstyle{color:#59990E;}.itemstyle{border-bottom:dotted 1px #59990E;margin-bottom:20px;} </style>
实现Silverlight程序
![](http://img1.51cto.com/attachment/200803/200803251206415825328.png)
<Grid x:Name="LayoutRoot" Background="White"><Grid.RowDefinitions><RowDefinition Height="55"></RowDefinition><RowDefinition Height="50"></RowDefinition><RowDefinition Height="35"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="*"></ColumnDefinition></Grid.ColumnDefinitions><Image Source="LiveSearch.png" Grid.Column="0"></Image><StackPanel Grid.Row="1" Orientation="Horizontal"><TextBox x:Name="txtQuery" Width="400" Height="35"Margin="50 0 0 0" BorderBrush="#3F7801"></TextBox><Button x:Name="btnSearch" Width="120" Height="35"Background="#62A21D" Margin="20 0 0 0"Content="Search" FontSize="16" Click="btnSearch_Click"></Button></StackPanel><TextBlock Grid.Row="2" Text="网页搜索结果" Foreground="#59990E"FontSize="16" Margin="20 0 0 0"></TextBlock> </Grid>
private void btnSearch_Click(object sender, RoutedEventArgs e) {LiveSearchWebServiceSoapClient client = new LiveSearchWebServiceSoapClient();client.DoSearchCompleted += new EventHandler<DoSearchCompletedEventArgs>(client_DoSearchCompleted);client.DoSearchAsync(this.txtQuery.Text); } void client_DoSearchCompleted(object sender, DoSearchCompletedEventArgs e) {if (e.Error == null){SearchResultItem[] results = e.Result as SearchResultItem[];HtmlElement result = HtmlPage.Document.GetElementById("result");foreach (SearchResultItem item in results){HtmlElement itemElement = HtmlPage.Document.CreateElement("div");itemElement.CssClass = "itemstyle";HtmlElement titleElement = HtmlPage.Document.CreateElement("a");titleElement.SetAttribute("href",item.Url);titleElement.SetAttribute("innerText",item.Title);HtmlElement descriptElement = HtmlPage.Document.CreateElement("div");descriptElement.SetAttribute("innerText",item.Description);HtmlElement urlElement = HtmlPage.Document.CreateElement("span");urlElement.SetAttribute("innerText",item.Url);urlElement.CssClass = "urlstyle";itemElement.AppendChild(titleElement);itemElement.AppendChild(descriptElement);itemElement.AppendChild(urlElement);result.AppendChild(itemElement);}} }
![](http://img1.51cto.com/attachment/200803/200803251206415834906.png)
结束语
本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67273
本文出自 51CTO.COM技术博客
转载于:https://www.cnblogs.com/GT_Andy/archive/2009/07/28/1922010.html
(25):Silverlight 2 综合实例之Live Search相关推荐
- 一步一步学Silverlight 2系列(25):综合实例之Live Search
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- (18):Silverlight 2 综合实例之RSS阅读器
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 一步一步学Silverlight 2系列(18):综合实例之RSS阅读器
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- C++ 个人银行账户管理 综合实例62.10_date
在网上找了很多例子,代码都不是循序渐进的,自己一点一点按章节把代码按照老师的要求都码好了,一点一点进步吧,因为一开始就去看别人整篇的代码实在很难,带给需要帮助的人,这是第六章第二个综合实例,加入Dat ...
- c# Windows窗体应用程序设计综合实例-------小游戏设计
c# Windows窗体应用程序设计综合实例(一)匹配游戏设计 上次的计时器运行效果图如下: = --------------------------------------------------- ...
- Silverlight+WCF 新手实例 象棋 主界面-棋谱-获取列表(三十八)
2019独角兽企业重金招聘Python工程师标准>>> 在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示 在Silverlight+WCF 新手实例 象棋 主界 ...
- Silverlight+WCF 新手实例 象棋 主界面-实时聊天区(二十五)
在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示 演示已更新到此节介绍:Silverlight+WCF 新手实例 象棋 介绍III(二十三) 本节连着Silverlight+WC ...
- Silverlight+WCF 新手实例 象棋 主界面-事件区-求和认输(三十二)
在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示 事隔几篇,我们又回到事件区,继续其它两个按钮事件,来张图吧: 在Silverlight+WCF 新手实例 象棋 主界面-事件区- ...
- Silverlight+WCF 新手实例 象棋 该谁下棋-B下A停(三十)
2019独角兽企业重金招聘Python工程师标准>>> 在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示 上上一节,就是二十八节:Silverlight+WCF ...
最新文章
- 从空间数据库中删除所有拓扑对象
- Log4j显示异常的奇怪问题
- Linux 用户和用户操作
- oracle_plseq客户端中文乱码
- 外媒解读乌克兰电网遭遇黑客事故
- Android opencv cvCvtColor()的转换
- [译] 这可能是 2018 年最好的一篇 PHP 性能测评(包含 5.6 到 7.2,以及 HHVM)
- python 小波去噪,用Pywavelet去噪信号?
- 3.2 python实战_线性回归的梯度下降法
- HTML手机上图片显示被压扁,在重新调整Web浏览器HTML |时,文本会被压扁CSS
- python lxml用法详解_Python-- lxml用法
- nested set model应用系列文章-基于后根跳跃遍历的规则匹配算法
- Python+OpenCV:二维直方图(2D Histograms)
- TextBar for Mac(菜单栏增强工具)支持m1
- mac电脑上的效率工具:alfred 4
- 华为ensp常用操作命令 网络人凭经验含泪总结
- 网络流专题(最大流与费用流)例题总结
- MSNMessenger忌讳用法大全(转)
- SEGGER RTT使用总结
- wps的ppt如何去掉插入表格的表头与表体之间的空白间隔