silverlight:Silverlight专题(14) - 基于Silverlight的Live Search图片搜索

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

  前言:

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

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

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

  本篇将带大家走进Silverlight+Live Image Search美妙世界

  再阅读本篇文章的前请先阅读上篇文章:Silverlight专题(12) - 基于SilverlightLive Search网页搜索

  另外Silverlight专题(13) - 基于SilverlightLive Search资讯搜索也有参考价值

  解决方案:

  效果展示图如下:

  Silverlight专题(14) - 基于Silverlight<img src='/icons/33436de.gif' />Live Search图片搜索 Silverlight专题(14) - 基于Silverlight<img src='/icons/33436de.gif' />Live Search图片搜索

  UI层如下:

  UI XAML

1<Grid x:Name="LayoutRoot" Background="#808080">
2   <Grid.RowDefinitions>
3     <RowDefinition Height="Auto"/>
4     <RowDefinition Height="Auto"/>
5     <RowDefinition/>
6   </Grid.RowDefinitions>
7   <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,20">
8     <local:WatermarkedTextBox Width="400" x:Name="QueryCtl" Watermark="Please Enter Your Query Keywords" VerticalContentAlignment="Center"/>
9     <Button x:Name="SearchBtnCtl" Margin="4,0" Width="120" Content="Search" Template="{StaticResource DefaultBtnTemplate}" Click="SearchBtnCtl_Click"/>
10   </StackPanel>
11   
12   <TextBlock x:Name="ImagesCountCtl" Grid.Row="1" HorizontalAlignment="Center" Width="720" Foreground="White"/>
13   
14   <ListBox Grid.Row="2" VerticalAlignment="Top" x:Name="ImageListCtl" Width="740" BorderThickness="0" Background="#808080" SelectionChanged="ImageListCtl_SelectionChanged">
15     <ListBox.ItemTemplate>
16       <DataTemplate>
17         <Border Background="White" CornerRadius="5" BorderThickness="5">
18           <StackPanel Width="160">
19             <TextBlock Text="{Binding Title}" HorizontalAlignment="Center"/>
20             <Image Height="120" Margin="8,10" Source="{Binding ThumbnailUrl}"/>
21           </StackPanel>
22         </Border>
23       </DataTemplate>
24     </ListBox.ItemTemplate>
25     <ListBox.ItemsPanel>
26       <ItemsPanelTemplate>
27         <toolkit:WrapPanel Width="720"/>
28       </ItemsPanelTemplate>
29     </ListBox.ItemsPanel>
30   </ListBox>
31</Grid>


  这里大家可能不理解应该是25~29行

  ListBoxItemsPanel是用来定义ListBoxListBoxItem应该如何放置

  p默认情况下是以垂直方式排列大家如果看过前面两篇文件就应该有这种感觉

  难道ListBoxItem只能垂直排列吗?

  不能行容纳几个Item吗?

  而ItemsPanel这个属性就可以帮你

  这里我使用了Silverlight Toolkit中WrapPanel

  WrapPanel特点是如果发现行已经不能容纳所有Items

  会自动换行于是就可以在ListBox中实现行有多个Items功能了

  底层代码如下:

  Code C#

1public Page
2{
3   InitializeComponent;
4   SetImagesStyle;
5   App.Current.Host.Content.Resized EventHandler(Content_Resized);
6}
7 
8void Content_Resized(object sender, EventArgs e)
9{
10   SetImagesStyle;
11}
12 
13privatevoid SearchBtnCtl_Click(object sender, RoutedEventArgs e)
14{
15   this.SearchBtnCtl.Content ="Searching";
16   this.SearchBtnCtl.IsEnabled =false;
17   LiveSearchPortTypeClient liveSearchClient = LiveSearchPortTypeClient;
18   SearchRequest liveSearchRequest = SearchRequest;
19   liveSearchRequest.Query =this.QueryCtl.Text.Trim;
20   liveSearchRequest.Version ="2.0";
21   liveSearchRequest.AppId ="44980C5CFA65792B3CDFF33A5CBF2CFAD17E3349";
22   liveSearchRequest.Market ="zh-cn";
23   liveSearchRequest.Sources = SourceType { SourceType.Image };
24   liveSearchRequest.Image = ImageRequest;
25   //最大只能到50
26   liveSearchRequest.Image.Count =40;
27   liveSearchRequest.Image.CountSpecied =true;
28   liveSearchClient.SearchAsync(liveSearchRequest);
29   liveSearchClient.SearchCompleted EventHandler<SearchCompletedEventArgs>(liveSearchClient_SearchCompleted);
30}
31 
32void liveSearchClient_SearchCompleted(object sender, SearchCompletedEventArgs e)
33{
34   List<LiveImageInfo> m_liveSearchImages = List<LiveImageInfo>;
35   SearchResponse liveSearchResponse = e.Result;
36   ImageResponse imageResponse = liveSearchResponse.Image;
37   (imageResponse !=null)
38   {
39     this.ImagesCountCtl.Text = String.Format("共有{0}个搜索结果", imageResponse.Total);
40     foreach (ImageResult result in imageResponse.Results)
41     {
42       lastDotIndex = result.MediaUrl.LastIndexOf('.');
43       ext = result.MediaUrl.Sub(lastDotIndex +1);
44       (ext "jpg"|| ext "png"|| ext "jpeg")
45       {
46         LiveImageInfo liveImage = LiveImageInfo;
47         liveImage.Title = result.Title;
48         liveImage.PageUrl = result.Url;
49         liveImage.MediaUrl = result.MediaUrl;
50         liveImage.ThumbnailUrl = result.Thumbnail.Url;
51         liveImage.DisplayUrl = result.DisplayUrl;
52         m_liveSearchImages.Add(liveImage);
53       }
54     }
55 
56     this.ImageListCtl.ItemsSource = m_liveSearchImages;
57   }
58 
59  
60   {
61     this.ImagesCountCtl.Text = String.Format("共有{0}个搜索结果", 0);
62     this.ImageListCtl.ItemsSource =null;
63   }
64   this.SearchBtnCtl.IsEnabled =true;
65   this.SearchBtnCtl.Content ="Search";
66}
67 
68privatevoid SetImagesStyle
69{
70   this.ImageListCtl.Height =0.95* App.Current.Host.Content.ActualHeight -100;
71}
72 
73privatevoid ImageListCtl_SelectionChanged(object sender, SelectionChangedEventArgs e)
74{
75   LiveImageInfo currImageInfo = (sender as ListBox).SelectedItem as LiveImageInfo;
76   (currImageInfo!=null)
77     HtmlPage.Window.Navigate( Uri(currImageInfo.PageUrl, UriKind.Absolute),"_blank");
78}


  大家请注意下底层代码第26行

  Live Image Search次性最多只能取50张图片(这是考虑了服务器负担作出限定)

  77行HtmlPage.Window.Navigate( Uri(currImageInfo.PageUrl, UriKind.Absolute),"_blank");

  用来打开个新网页窗口并跳转到图片所在网页

  整理总结:

  Silverlight专题(12) - 基于SilverlightLive Search网页搜索

  Silverlight专题(13) - 基于SilverlightLive Search资讯搜索

  Silverlight专题(14) - 基于SilverlightLive Search图片搜索

   3篇文章抛砖引玉粗略介绍了下如果利用Silverlight网络通信来利用Live Search资源

  其中简单介绍了Live Search最新2.0 Beta版API

  指出了当前API地方并做了修正

  此外利用数据绑定方便展示获取得到结果

  相关资源:

  Silverlight专题-引言

  Silverlight专题(1)-构建第个Silverlight应用

  Silverlight专题(2)-添加并使用自定义字体

  Silverlight专题(3)-使用Deep Zoom Composer

  Silverlight专题(4)-自定义提示信息



  Silverlight专题(5)-Silverlight 2 RC0更新内容

  Silverlight专题(6)-自定义提示Plugin安装信息

  Silverlight专题(7)-基本Control控件介绍

  Silverlight专题(8)-布局介绍

  Silverlight专题(9)-WCF通信

  Silverlight专题(10)-WCF通信(2)

  Silverlight专题(11) - Silverlight单元测试

  Silverlight专题(12) - 基于SilverlightLive Search网页搜索

  Silverlight专题(13) - 基于SilverlightLive Search资讯搜索

  Silverlight专题(14) - 基于SilverlightLive Search图片搜索

  Silverlight专题(15) - 你自己视频播放器的自定义MoveToPoSlider



Tags:  silverlight2 silverlight.2.0 silverlight是什么 silverlight

延伸阅读

最新评论

发表评论