silverlight:Silverlight专题(12) - 基于Silverlight的Live Search网页搜索

  本文举例源代码或素材下载

  前言:  最近几天微软Live Search公布了重新架构了Live Search API(版本为2.0 Beta)

  该API律属于微软最新Live Search Service项目 – Silk Road(丝绸的路)

  那么如何在Silverlight中Live Search Service呢来进行网页图片资讯等搜索呢?

  本篇以及后面几篇文章将带大家走进Silverlight+Live Search美妙世界

  准备工作:  请到http://search.live.com/developers申请个AppID这个AppID是你访问Live Search验证信息

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

  用你hotmail或者msn账号登陆就可以申请了

  (以前Live Search API 1.1 AppID申请也完成了他使命当然大家还可以继续使用以前AppID来访问1.1版本Live Search Service啦)

  另外大家可以下载最新SDK: Live Search SDK 2.0 Beta

  该SDK包含了API以及示范例子代码(包括VB和C#版本)

  Live Search 2.0共有 3种访问协议:

  JSON

  XML

  SOAP

  在Live Search系列文章中我将直使用SOAP协议来访问其使用C#访问非常便捷

  大家可以根据自己项目需要使用合适协议

  下面让我们开始

  创建个Silverlight项目

  添加个Service Reference如下

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

  其中Address中地址格式如下:http://api.search.live.net/search.wsdl?AppID=你申请AppID

  点击Go如果你输入地址正确而且有网络连接应该就能搜索到和上图LiveSearchService

  填写你希望Namespace并点击OK等待数秒后会弹出如下窗口

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

  不用管它点OK就可以了

  查看下这个Service提供对象接口

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

  这里面没有LiveSearchService这个对象也就是你下载到SDK中访问LiveSearchService方式以及不样了

  (两天前还有昨晚我再次尝试时候就没有了这样做原因相必是为了和WCF兼容或者是已经采用WCF来提供Service接口了)

  取而代的是LiveSearchPortTypeClient大家把它当成WebClient类似东西就很容易领悟到它方式了

  也就是说最新Document和最新提供LiveSearchService接口有些出入不过我已经把这个问题解决

  解决方案:  UI展示代码如下:

  Code

1   <UserControl.Resources>
2     <ControlTemplate x:Key="DefaultBtnTemplate" TargetType="Button">
3       <Border CornerRadius="8" x:Name="border">
4         <Border.Background>
5           <LinearGradientBrush EndPo="0.5,1" StartPo="0.5,0">
6             <GradientStop Color="#FFB2E09A"/>
7             <GradientStop Color="#FF4E942F" Off="0.7"/>
8           </LinearGradientBrush>
9         </Border.Background>
10         <TextBlock Margin="{TemplateBinding Padding}" Foreground="White" Text="{TemplateBinding Content}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
11         <vsm:VisualStateManager.VisualStateGroups>
12           <vsm:VisualStateGroup x:Name="FocusStates">
13             <vsm:VisualState x:Name="Focused"/>
14             <vsm:VisualState x:Name="Unfocused"/>
15           </vsm:VisualStateGroup>
16           <vsm:VisualStateGroup x:Name="CommonStates">
17             <vsm:VisualState x:Name="Normal"/>
18             <vsm:VisualState x:Name="MouseOver">
19             </vsm:VisualState>
20             <vsm:VisualState x:Name="Pressed"/>
21             <vsm:VisualState x:Name="Disabled"/>
22           </vsm:VisualStateGroup>
23         </vsm:VisualStateManager.VisualStateGroups>
24       </Border>
25     </ControlTemplate>
26   </UserControl.Resources>
27   <Grid x:Name="LayoutRoot" Background="#3c3c3c">
28     <Grid HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,10">
29       <Grid.RowDefinitions>
30         <RowDefinition Height="Auto"/>
31         <RowDefinition Height="Auto"/>
32         <RowDefinition Height="Auto"/>
33       </Grid.RowDefinitions>
34       <Grid.ColumnDefinitions>
35         <ColumnDefinition Width="Auto"/>
36         <ColumnDefinition Width="Auto"/>
37       </Grid.ColumnDefinitions>
38       
39       <TextBox x:Name="KeywordsCtl" Width="400"/>
40       <Button x:Name="SearchBtnCtl" Content="Search" Padding="30,5" Margin="4,0,2,0" Template="{StaticResource DefaultBtnTemplate}" Grid.Column="1" Click="SearchBtnCtl_Click"/>
41       <TextBlock Foreground="White" Grid.Row="1" x:Name="WebNumCtl" Margin="2"/>
42       <ListBox x:Name="WebPanelCtl" Grid.Row="2" Grid.ColumnSpan="2" Margin="2,0" BorderThickness="0" Background="#3c3c3c" Height="500">
43         <ListBox.ItemTemplate>
44           <DataTemplate>
45             <StackPanel Width="480">
46               <HyperlinkButton Content="{Binding Title}" NavigateUri="{Binding OriginalUrl}" TargetName="_blank"/>
47               <TextBlock TextWrapping="Wrap" Foreground="White" Text="{Binding Description}" Margin="0,2"/>
48               <TextBlock Text="{Binding DisplayUrl}" Foreground="Green" FontSize="10"/>
49             </StackPanel>
50           </DataTemplate>
51         </ListBox.ItemTemplate>
52       </ListBox>
53     </Grid>
54   </Grid>


  KeywordsCtl用于搜索词输入

  SearchBtnCtl为搜索按钮

  WebNumCtl用来展示共搜索到多少条新闻

  WebPanelCtl用于展示得到搜索结果

  其中WebPanelCtl用到了DataBinding(数据绑定)

  底层代码:

  Code

1privatevoid SearchBtnCtl_Click(object sender, RoutedEventArgs e)
2{
3   LiveSearchPortTypeClient liveSearchClient = LiveSearchPortTypeClient;
4   SearchRequest webRequest = SearchRequest;
5   webRequest.AppId ="44980C5CFA65792B3CDFF33A5CBF2CFAD17E3349";
6   webRequest.Market ="zh-CN";
7   webRequest.Version ="2.0";
8   webRequest.Sources = SourceType { SourceType.Web };
9   webRequest.Query =this.KeywordsCtl.Text.Trim;
10   webRequest.Options = SearchOption { SearchOption.EnableHighlighting };
11   webRequest.Web= LiveSearchServiceRef.WebRequest;
12   webRequest.Web.Count =30;
13   webRequest.Web.CountSpecied =true;
14 
15   liveSearchClient.SearchAsync(webRequest);
16   liveSearchClient.SearchCompleted EventHandler<SearchCompletedEventArgs>(liveSearchClient_SearchCompleted);
17}
18 
19void liveSearchClient_SearchCompleted(object sender, SearchCompletedEventArgs e)
20{
21   SearchResponse liveSearchResponse = e.Result;
22   LiveSearchServiceRef.WebResponse webResponse = liveSearchResponse.Web;
23   this.WebNumCtl.Text = String.Format("共{0}条搜索结果",webResponse.Total);
24   List<WebInfo> m_webInfoList = List<WebInfo>;
25   (webResponse.Results.Length >0)
26   {
27     foreach (WebResult webResult in webResponse.Results)
28     {
29       WebInfo webInfo = WebInfo;
30       webInfo.Title = webResult.Title;
31       webInfo.Description = webResult.Description;
32       webInfo.PublishDateTime = webResult.DateTime;
33       webInfo.OriginalUrl = webResult.Url;
34       webInfo.DisplayUrl = webResult.DisplayUrl;
35       m_webInfoList.Add(webInfo);
36     }
37 
38     this.WebPanelCtl.ItemsSource = m_webInfoList;
39   }
40}


  SearchRequest用来定义AppID以及搜索市场使用搜索版本等

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索

  Query用于提供给LiveSearchService搜索词

  Sources用来定义搜索来源目前共有

  ImageInstantAnswerNewsPhoneBookRelatedSearchSpellCheckWeb 7种美国市场还有AD

  (注意:你在SearchRequest定义了哪几种搜索源那么SearchResponseResponse类型也就只有那几种)

  代码1213行用于定义SearchResponse返回多少条结果

  LiveSearchPortTypeClient通过异步方式SearchRequest

  LiveSearchPortTypeClient将通过SearchCompleted这个事件回传给客户端查询结果也就是这里SearchResponse

  38行将获得数据绑定给WebPanelCtl这样我们就得到了查询信息了

  其中WebInfo对象是用来存储获取网页信息其定义如下

  WebInfo

1 LiveSearchWeb4Silverlight
2{
3   public WebInfo
4   {
5     public Title { get; ; }
6     public Description { get; ; }
7     public PublishDateTime { get; ; }
8     public OriginalUrl { get; ; }
9     public DisplayUrl { get; ; }
10   }
11}


  效果展示:  你可以在搜索框中输入些搜索词来得到结果

  我些搜索结果展示以及和Live Search比较

Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索Silverlight专题(12) - 基于Silverlight<img src='/icons/31874de.gif' />Live Search网页搜索



  文章来源: http://www.cnblogs.com/ibillguo/archive



Tags:  silverlight2 silverlight.2.0 silverlight是什么 silverlight

延伸阅读

最新评论

发表评论