一步一步学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。

TerryLee_Silverlight2_0115

申请完成后应用程序ID大约在10分钟左右生效。关于Live Search API的有关详细信息,请大家参考这里。

编写ASMX

直接调用API,返回的信息可能有很多,为了简单起见,我们对返回的结果做一些处理,编写一个SearchResultItem类:

public class SearchResultItem
{public string Title { get; set; }public string Url { get; set; }public string Description { get; set; }
}

添加对Live Search API的Service引用,地址为:http://soap.search.live.com/webservices.asmx?wsdl。

TerryLee_Silverlight2_0118

在ASMX中对返回的结果进行一些处理,Silverlight程序最后将直接调用ASMX。在调用Live Search时需要指定应用程序ID以及本地化的信息等,查询的参数将在Silverlight程序中调用时传入。

[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();
}

测试一下我们的服务是否正常:

TerryLee_Silverlight2_0116 

修改测试页

在测试ASPX中,修改Silverlight插件的样式控制,并添加一个div用来显示搜索的结果:

<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程序

编写一个简单的Silverlight界面,使其看起来如图所示:

TerryLee_Silverlight2_0117

XAML声明如下:

<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>

在Silverlight项目中添加对于ASMX的引用,并编写“Search”按钮的实现,对于如何调用ASMX,可以参考一步一步学Silverlight 2系列(15):数据与通信之ASMX。动态创建DOM结构,并将结果显示出来:

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);}}
}

运行看一下效果,查询博客园:

TerryLee_Silverlight2_0119

结束语

本文综合了前面关于浏览器集成以及数据与通信部分的内容,开发了一个综合的示例——Live Search。你可以从这里下载本文示例代码。

作者:TerryLee
出处:http://terrylee.cnblogs.com 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: [03]  银光点亮世界

转载于:https://www.cnblogs.com/meimao5211/p/3427976.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360244.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gateway中的局部过滤器_Spring Cloud Gateway中的过滤器工厂:重试过滤器

Spring Cloud Gateway基于Spring Boot 2&#xff0c;是Spring Cloud的全新项目&#xff0c;该项目提供了一个构建在Spring 生态之上的API网关。本文基于的Spring Cloud版本为Finchley M9&#xff0c;Spring Cloud Gateway对应的版本为2.0.0.RC1。Spring Cloud Gateway入门一文介…

MySql命令行基本操作

启动mysql服务&#xff1a; net start mysql关闭mysql服务&#xff1a; net stop mysql命令行登陆mysql&#xff1a;mysql -h localhost -u root -p admin 命令行登陆mysql&#xff1a;mysql -uroot -padmin 退出mysql控制台&#xff1a;quit或者exit查看mysql控制台当前信息&a…

mysql 5.7 启动脚本_MySQL数据库 5.7 启动脚本

本文主要向大家介绍了MySQL数据库 5.7 启动脚本&#xff0c;通过具体的内容向大家展现&#xff0c;希望对大家学习MySQL数据库有所帮助。最近这段时间&#xff0c;在看mysql&#xff0c;安装了&#xff0c;也应用过&#xff0c;对于生产环境中&#xff0c;一般都选择使用source…

cocos2d-x for android配置 运行 Sample on Linux OS

1.从http://www.cocos2d-x.org/download下载稳定版 比如cocos2d-x-2.2 2.解压cocos2d-x-2.2.zip,比如本文将其解压到 /opt 目录下 3.运行 android-buildsetup.sh,运行之前需要先设置3个环境变量,如将以下变量写到文件 /etc/profile中 export ANDROID_SDK_ROOT/opt/android-sdk-…

转变馆藏

您是否曾经想替换过HashSet或HashMap使用的equals和hashCode方法&#xff1f; 或者有一个List的一些元素类型伪装成的List相关类型的&#xff1f; 转换集合使这成为可能&#xff0c;并且本文将展示如何实现。 总览 转换集合是LibFX 0.3.0的一项功能&#xff0c;该功能将在今天…

mysql 保证事物完整性_数据库高并发请求,如何保证数据完整性?详解MySQL/InnoDB的加锁...

本文是对MySQL/InnoDB中,乐观锁、悲观锁、共享锁、排它锁、行锁、表锁、死锁概念的理解&#xff0c;这些在面试中也经常遇到&#xff0c;如数据库高并发请求&#xff0c;如何保证数据完整性&#xff1f;今天我查阅资料进行了MySQL/InnoDB中加锁知识点的汇总&#xff0c;这样也会…

Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo

1、新建Dll工程 2、Dll工程全部代码 library SubMain;{ Important note about DLL memory management: ShareMem must be thefirst unit in your librarys USES clause AND your projects (selectProject-View Source) USES clause if your DLL exports any procedures orfunct…

Java擦除

概述&#xff1a; Java泛型在使用过程有诸多的问题&#xff0c;如不存在List<String>.class, List<Integer>不能赋值给List<Number>&#xff08;不可协变&#xff09;&#xff0c;奇怪的ClassCastException等。 正确的使用Java泛型需要深入的了解Java的一些概…

mysql数据库相互备份_MySQL的本地备份和双机相互备份脚本

先修改脚本进行必要的配置,然后以root用户执行.1. 第一执行远程备份时先用 first参数.2. 本地备份用local参数3. 远程备份不用参数注意:需要在另一主机上的Mysql用户用添加用户..需要配置的地方:# define host and mysql passwordREMOTE_HOST"" #远程主机名或IPREMOT…

Spring Boot和Swagger UI

我已经一年没有从头开始开发Spring Web应用程序了&#xff0c;如果我不参加QA自动化工程师的培训&#xff0c;那么这段时间甚至会更长。 由于这个原因&#xff0c;我开发了一个示例REST应用程序。 除了Swagger&#xff0c;一切对我来说都很熟悉。 因此&#xff0c;我将描述我在…

mysql5.7.22打不开_windows下mysql-5.7.22-winx64突然启动不了,报错Could not open log file

本文摘自classinstance.cn。windows下mysql-5.7.22-winx64突然启动不了&#xff0c;感觉启动几秒钟后就自己关闭了&#xff0c;看了下启动日志&#xff1a;2019-08-25T10:57:08.389404Z 0 [Warning] option wait_timeout: unsigned value 31536000 adjusted to 21474832019-08-…

HDU1530 最大流问题

第一次写Dinic 然后贴一下 最基础的网络流问题 嘎嘎: #include <iostream> #include<cstdio> #include<string.h> #include<queue> using namespace std; const int M205; __int64 map[M][M]; int n,m,dist[M]; queue<int>q; void readdate() {_…

把python37添加到环境变量配置_关于在win 10上成功创建一个Django项目时遇到django-admin的手动配置环境变量问题。...

前言初学Python Web 在创建第一个Djang项目的时候出现了很多的问题&#xff0c;今天和大家分享并记录一下这次艰难的历程&#xff01;一、官网下载Python以及安装Django1、Python的下载安装链接&#xff1a;大家最好使用谷歌浏览器&#xff0c;因为翻译的很到位(大家下载最新版…

在Ant中显示路径

在博客文章Java and Ant Properties Refresher和Ant <echoproperties /> Task中 &#xff0c;我写了一篇关于如何了解Ant构建如何看到属性的文章&#xff0c;这有助于更好地理解构建。 通常情况下&#xff0c;在构建过程中看到构建中使用的各种路径也很有价值&#xff0c…

如何删除数据库中的所有用户表(表与表之间有外键关系)

1、由表名求字段名 create proc up_008(table varchar(20)) as begin declare sql varchar(99) select sql\select name from syscolumns where idobject_id(\ select sqlsql\\\\table\\\\\)\ --select sql exec(SQL) end exec up_008 a_idx2 2、编程删…

Azure开发者任务之一:解决Azure Storage Emulator初始化失败

初学Windows Azure&#xff1a; 我打算开始学习Windows Azure。我安装了Azure SDK&#xff0c;然后在“Cloud”标签下选择Windows Azure模板&#xff0c;创建了一个项目&#xff0c;然后又创建了一个Web角色。 在aspx文件上&#xff0c;我只添加了一个标签&#xff0c;但是当我…

关于Java里如何跳出一个多重循环

一般我们要跳出一个循环&#xff0c;用break就OK了&#xff0c;比如&#xff1a; 1 for(int i1;i<5;i){ 2   if&#xff08;条件&#xff09; 3     break&#xff1b; 4   //一些代码 5 } 但是如果这时候&#xff0c;在这一层循环外面还有一层循环的话&#…

CCRC认证对企业的作用?

CCRC认证&#xff08;中国网络安全审查认证&#xff09;是针对网络产品和服务的安全审查制度&#xff0c;它对企业的作用主要体现在以下几个方面&#xff1a; 1. 提升产品安全性 CCRC认证要求企业对其网络产品和服务进行全面的安全审查&#xff0c;确保符合国家网络安全标准和…

java运行python3_python写脚本并用java调用python(三)

1)编写mytest.py完成一个简单加法计算# coding:utf8#def 表示一个方法 adderdef adder(a, b):return ab#这里执行adder方法并打印出结果print adder(1,2)2)运行以上脚本方式如图12 3 打印成功&#xff01;3)java调用python脚本的两种方式Process process Runtime.getRuntime(…

Hibernate教程– ULTIMATE指南(PDF下载)

编者注&#xff1a;在本文中&#xff0c;我们提供了全面的Hibernate教程。 Hibernate ORM&#xff08;简称Hibernate&#xff09;是一个对象关系映射框架&#xff0c;它有助于将面向对象的域模型转换为传统的关系数据库。 Hibernate通过用高级对象处理功能代替直接与持久性相关…