热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Silverlight学习之调用bing搜索引擎进行网络搜索

发布时间:2010年06月10日点击数: 佚名

这一篇主要讲解Silverlight中如何调用bing搜索引擎进行网络搜索,关于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

  1. <UserControl x:Class="SilverlightMySearch.MainPage" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     mc:Ignorable="d" 
  7.     d:DesignHeight="600" d:DesignWidth="800"> 
  8.      
  9.     <Canvas> 
  10.         <TextBox Name="searchString" Width="300" Height="30" Canvas.Left="50" Canvas.Top="50" FontSize="16"></TextBox> 
  11.         <Button Name="doSearch" Width="80" Canvas.Left="400" Canvas.Top="50" Height="30" Content="Search" Background="Red" Click="doSearch_Click"></Button> 
  12.         <ListBox Name="searchResultList" Height="500" Width="700" Canvas.Top="100" Canvas.Left="50" Visibility="Collapsed"> 
  13.  
  14.         </ListBox> 
  15.         <Canvas.Background> 
  16.             <ImageBrush ImageSource="Image/bing.jpg"></ImageBrush> 
  17.         </Canvas.Background> 
  18.     </Canvas> 
  19. </UserControl> 

MainPage.xaml.cs

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using System.Windows.Browser; 
  13. using SilverlightMySearch.MySearch; 
  14.  
  15. namespace SilverlightMySearch 
  16.     public partial class MainPage : UserControl 
  17.     { 
  18.         //初始化一个客户端 
  19.         LiveSearchPortTypeClient myCilent = new LiveSearchPortTypeClient(); 
  20.         public MainPage() 
  21.         { 
  22.             InitializeComponent(); 
  23.             //注册一个事件,用来处理搜索完成之后的事情 
  24.             myCilent.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(myCilent_SearchCompleted); 
  25.         } 
  26.  
  27.         private void doSearch_Click(object sender, RoutedEventArgs e) 
  28.         { 
  29.             searchResultList.Visibility = doSearch.Visibility; 
  30.             SearchRequest sr = new SearchRequest(); 
  31.             //在bing上注册的AppId 
  32.             sr.AppId = "D30D45ADA751FB5B1AEC7B0D8D5852825498D57F"
  33.             //AppId版本 
  34.             sr.Version = "2.0"
  35.             //搜索得到的资源,这边只搜索网页,当然可以通过SourceType的设定不同取得如Image、News、Video等 
  36.             sr.Sources = new SourceType[] {SourceType.Web }; 
  37.             //搜索内容,即关键字 
  38.             sr.Query = searchString.Text.Trim().ToString(); 
  39.             sr.Web = new SilverlightMySearch.MySearch.WebRequest() 
  40.             { 
  41.                 CountSpecified=true
  42.                 //这边先让它显示50条 
  43.                 Count=50 
  44.             }; 
  45.             //执行搜索 
  46.             myCilent.SearchAsync(sr); 
  47.         } 
  48.         /// <summary> 
  49.         /// 搜索完成之后将结果加载到页面上的ListBox中 
  50.         /// </summary> 
  51.         /// <param name="sender"></param> 
  52.         /// <param name="e"></param> 
  53.         void myCilent_SearchCompleted(object sender, SearchCompletedEventArgs e) 
  54.         { 
  55.             searchResultList.Items.Clear(); 
  56.             foreach(var item in e.Result.Web.Results) 
  57.             { 
  58.                 ListBoxItem lbi = new ListBoxItem(); 
  59.                 lbi.Height = 120; 
  60.                 lbi.Width = searchResultList.Width; 
  61.                 StackPanel sp = new StackPanel(); 
  62.                 //标题模块 
  63.                 TextBlock title = new TextBlock(); 
  64.                 title.Text = item.Title; 
  65.                 title.TextWrapping = TextWrapping.Wrap; 
  66.                 title.FontSize = 20; 
  67.                 title.Foreground = new SolidColorBrush(Colors.Red); 
  68.                 sp.Children.Add(title); 
  69.                 //正文模块 
  70.                 TextBlock content = new TextBlock(); 
  71.                 content.FontSize = 15; 
  72.                 content.TextWrapping = TextWrapping.Wrap; 
  73.                 content.Text = item.Description; 
  74.                 content.Foreground = new SolidColorBrush(Colors.Gray); 
  75.                 sp.Children.Add(content); 
  76.                 //url地址模块 
  77.                 TextBlock url = new TextBlock(); 
  78.                 url.FontSize = 15; 
  79.                 url.TextWrapping = TextWrapping.Wrap; 
  80.                 url.Text = item.DisplayUrl; 
  81.                 url.Foreground = new SolidColorBrush(Colors.Green); 
  82.                 sp.Children.Add(url); 
  83.  
  84.                 lbi.Content = sp; 
  85.                 searchResultList.Items.Add(lbi); 
  86.                 //点击标题后弹出具体网页页面 
  87.                 title.Tag = url.Text; 
  88.                 title.MouseEnter+=new MouseEventHandler(title_MouseEnter); 
  89.                 title.MouseLeave+=new MouseEventHandler(title_MouseLeave); 
  90.                 title.MouseLeftButtonUp+=new MouseButtonEventHandler(title_MouseLeftButtonUp); 
  91.             } 
  92.         } 
  93.         void title_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
  94.         { 
  95.             //用于弹出浏览器窗体 
  96.             HtmlPopupWindowOptions option = new HtmlPopupWindowOptions(); 
  97.             HtmlPage.PopupWindow((new UriBuilder((sender as TextBlock).Tag.ToString())).Uri, "", option); 
  98.         } 
  99.         void title_MouseEnter(object sender, MouseEventArgs e) 
  100.         { 
  101.             TextBlock tb = sender as TextBlock; 
  102.             //鼠标放到TextBlock显示下划线 
  103.             tb.TextDecorations = TextDecorations.Underline; 
  104.             //鼠标放到TextBlock显示手型 
  105.             tb.CaptureMouse(); 
  106.             tb.Cursor = Cursors.Hand; 
  107.         } 
  108.         void title_MouseLeave(object sender, MouseEventArgs e) 
  109.         { 
  110.             TextBlock tb = sender as TextBlock; 
  111.             tb.Cursor = null
  112.             tb.TextDecorations = null
  113.         } 
  114.     } 

这边其实也没什么好讲的,代码中都加了注释。
首先需要添加两个命名空间
using System.Windows.Browser;
using SilverlightMySearch.MySearch;
一个用来弹出新页面,一个用来调用bing API。其他的没什么好讲了,读者只需将代码完全覆盖你项目中对应的两个文件即可,确保你的电脑已经连网,运行出现如下效果:

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

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

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

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作