主要讲解Silverlight中如何调用bing搜索引擎进行网络搜索
为了更好的进行知识的快速普及,调动最广大人民的积极性,我们还是采用以前的教学方式--图、码、文并进,手把手的教学方式。
首先确保你的电脑上已经安装了VS2010。
打开2010,New一个SilverlightApplication,取名SilverlightMySearch
如下图所示:

弹出下图所示界面,点击ok即可

接下来打开bing首页http://cn.bing.com/,出现下图页面

在地址栏中输入http://cn.bing.com/developer,出现下图页面

点击Get an AppID中的链接进入到如下页面

这边你如果没有账号可以先注册一个,有的话直接登录,注册完或者登录以后再进入http://cn.bing.com/developer页面,这时点击Get an AppID可以进入下图所示页面:

首次进入还没有AppID可以点选右边链接进入如下页面,填写必要的信息来取得一个AppID

创建成功取得AppID如下图所示,这个AppID以后都可以拿来使用

至此AppID的取得讲解完毕。
下面讲解如何在VS2010中添加这个引用,打开SilverlightMySearch,右击工程目录References选择Add Service Reference,在Address中填入http://api.search.live.net/search.wsdl?AppID=D30D45ADA751FB5B1AEC7B0D8D5852825498D57F,NameSpace这边取名MySearch,填写完点击OK,界面图如下:

点击完ok以后如果添加成功会出现下图所示,在工程中多了两个文件

添加引用完毕。
现在开始写代码,打开MainPage.xaml和MainPage.xaml.cs文件,这边我先把完成后的代码全部贴出来,再具体解释
MainPage.xaml
代码
<UserControl x:Class="SilverlightMySearch.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800">
<Canvas>
<TextBox Name="searchString" Width="300" Height="30" Canvas.Left="50" Canvas.Top="50" FontSize="16"></TextBox>
<Button Name="doSearch" Width="80" Canvas.Left="400" Canvas.Top="50" Height="30" Content="Search" Background="Red" Click="doSearch_Click"></Button>
<ListBox Name="searchResultList" Height="500" Width="700" Canvas.Top="100" Canvas.Left="50" Visibility="Collapsed">
</ListBox>
<Canvas.Background>
<ImageBrush ImageSource="Image/bing.jpg"></ImageBrush>
</Canvas.Background>
</Canvas>
</UserControl>
MainPage.xaml.cs
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using SilverlightMySearch.MySearch;
namespace SilverlightMySearch
{
public partial class MainPage : UserControl
{
//初始化一个客户端
LiveSearchPortTypeClient myCilent = new LiveSearchPortTypeClient();
public MainPage()
{
InitializeComponent();
//注册一个事件,用来处理搜索完成之后的事情
myCilent.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(myCilent_SearchCompleted);
}
private void doSearch_Click(object sender, RoutedEventArgs e)
{
searchResultList.Visibility = doSearch.Visibility;
SearchRequest sr = new SearchRequest();
//在bing上注册的AppId
sr.AppId = "D30D45ADA751FB5B1AEC7B0D8D5852825498D57F";
//AppId版本
sr.Version = "2.0";
//搜索得到的资源,这边只搜索网页,当然可以通过SourceType的设定不同取得如Image、News、Video等
sr.Sources = new SourceType[] {SourceType.Web };
//搜索内容,即关键字
sr.Query = searchString.Text.Trim().ToString();
sr.Web = new SilverlightMySearch.MySearch.WebRequest()
{
CountSpecified=true,
//这边先让它显示50条
Count=50
};
//执行搜索
myCilent.SearchAsync(sr);
}
/// <summary>
/// 搜索完成之后将结果加载到页面上的ListBox中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void myCilent_SearchCompleted(object sender, SearchCompletedEventArgs e)
{
searchResultList.Items.Clear();
foreach(var item in e.Result.Web.Results)
{
ListBoxItem lbi = new ListBoxItem();
lbi.Height = 120;
lbi.Width = searchResultList.Width;
StackPanel sp = new StackPanel();
//标题模块
TextBlock title = new TextBlock();
title.Text = item.Title;
title.TextWrapping = TextWrapping.Wrap;
title.FontSize = 20;
title.Foreground = new SolidColorBrush(Colors.Red);
sp.Children.Add(title);
//正文模块
TextBlock content = new TextBlock();
content.FontSize = 15;
content.TextWrapping = TextWrapping.Wrap;
content.Text = item.Description;
content.Foreground = new SolidColorBrush(Colors.Gray);
sp.Children.Add(content);
//url地址模块
TextBlock url = new TextBlock();
url.FontSize = 15;
url.TextWrapping = TextWrapping.Wrap;
url.Text = item.DisplayUrl;
url.Foreground = new SolidColorBrush(Colors.Green);
sp.Children.Add(url);
lbi.Content = sp;
searchResultList.Items.Add(lbi);
//点击标题后弹出具体网页页面
title.Tag = url.Text;
title.MouseEnter+=new MouseEventHandler(title_MouseEnter);
title.MouseLeave+=new MouseEventHandler(title_MouseLeave);
title.MouseLeftButtonUp+=new MouseButtonEventHandler(title_MouseLeftButtonUp);
}
}
void title_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//用于弹出浏览器窗体
HtmlPopupWindowOptions option = new HtmlPopupWindowOptions();
HtmlPage.PopupWindow((new UriBuilder((sender as TextBlock).Tag.ToString())).Uri, "", option);
}
void title_MouseEnter(object sender, MouseEventArgs e)
{
TextBlock tb = sender as TextBlock;
//鼠标放到TextBlock显示下划线
tb.TextDecorations = TextDecorations.Underline;
//鼠标放到TextBlock显示手型
tb.CaptureMouse();
tb.Cursor = Cursors.Hand;
}
void title_MouseLeave(object sender, MouseEventArgs e)
{
TextBlock tb = sender as TextBlock;
tb.Cursor = null;
tb.TextDecorations = null;
}
}
}
这边其实也没什么好讲的,代码中都加了注释。
首先需要添加两个命名空间
using System.Windows.Browser;
using SilverlightMySearch.MySearch;
一个用来弹出新页面,一个用来调用bing API。其他的没什么好讲了,读者只需将代码完全覆盖你项目中对应的两个文件即可,确保你的电脑已经连网,运行出现如下效果:

点击第一个搜索结果的标题出现如下页面:

这边在页面上我加了一个bing的背景图片

有需要的可以将它下载下来,再根据MainPage.xaml中的具体路径将其添加到相应的工程目录中,如果不需要,请将MainPage.xaml中的背景路径删除,确保编译通过。
至此,整个流程讲解完毕,谢谢大家!









相关课程: