silverlight中使用Bing搜索

很多大型网站提供了对外的接口,比如Bing,这样我们就可以在我们的使用Bing的API来使用搜索功能。

  首先我们需要一个Bing appID 来使用Bing 的API,在http://www.bing.com/developers/createapp.aspx可以获得Bing appID .

给Silverlight项目中添加Service References: http://api.search.live.net/search.wsdl?AppID=******.

XAML:

代码

     < Grid  x:Name ="LayoutRoot" >
        
< Grid.RowDefinitions >
            
< RowDefinition  Height ="100" ></ RowDefinition >
            
< RowDefinition  Height ="*" ></ RowDefinition >
        
</ Grid.RowDefinitions >
        
< StackPanel  Orientation ="Horizontal" >
            
< Image  x:Name ="BingLogoImage"  Source ="binglogo.png"  Width ="100"  Stretch ="Uniform"  Margin ="3"  VerticalAlignment ="Center" ></ Image >
            
< TextBox  x:Name ="SearchTextBox"  Width ="150"  Margin ="3"  VerticalAlignment ="Center"   ></ TextBox >
            
< Button  x:Name ="SearchButton"  Click ="SearchButton_Click"  Content ="Search!"  HorizontalAlignment ="Center"  VerticalAlignment ="Center" ></ Button >
        
</ StackPanel >
        
< ListBox  x:Name ="ResultListBox"  Grid.Row ="1"  Margin ="3" >
            
< ListBox.ItemTemplate >
                
< DataTemplate >
                    
< Grid >
                        
< Grid.RowDefinitions >
                            
< RowDefinition ></ RowDefinition >
                            
< RowDefinition ></ RowDefinition >
                            
< RowDefinition ></ RowDefinition >
                        
</ Grid.RowDefinitions >
                        
< TextBlock  Text =" {Binding Title} "  FontWeight ="Bold"  FontSize ="15"  TextDecorations ="Underline"  Foreground ="#007fc6" ></ TextBlock >
                        
< TextBlock  Text =" {Binding Description} "  Grid.Row ="1"  TextWrapping ="Wrap"  Width ="400"  HorizontalAlignment ="Left" ></ TextBlock >
                        
< TextBlock  Text =" {Binding Uri} "  Grid.Row ="2"  Foreground ="#568e71" ></ TextBlock >
                    
</ Grid >
                
</ DataTemplate >
            
</ ListBox.ItemTemplate >
        
</ ListBox >
    
</ Grid >

 

  XAML.cs

代码

         private   void  SearchButton_Click( object  sender, RoutedEventArgs e)
        {
            BingService.LiveSearchPortTypeClient soapClient 
=   new  SilverBing.BingService.LiveSearchPortTypeClient();
            SearchRequest request 
=   new  SearchRequest();
            request.AppId 
=   " 61691gyt195F23456469594BAA33450A99CF3522 " ;
            request.Sources 
=   new  SourceType[] { SourceType.Web };

            
if  (SearchTextBox.Text  !=   string .Empty)
            {
                request.Query 
=  SearchTextBox.Text;
                soapClient.SearchCompleted 
+=   new                                                EventHandler < SearchCompletedEventArgs > (soapClient_SearchCompleted);
                soapClient.SearchAsync(request);
            }
        }

        
void  soapClient_SearchCompleted( object  sender, SearchCompletedEventArgs e)
        {
            SearchResponse response 
=  e.Result;
            
if  (response.Web.Results.Count()  >   0 )
            {
                var results 
=  from result  in  response.Web.Results
                              select 
new  BingSearchResult 
                              { 
                                  Title 
=  result.Title, 
                                  Uri 
=  result.Url, 
                                  Description 
=  result.Description 
                              };
                ResultListBox.ItemsSource 
=  results.ToList();
            }
        }

 

 

  结果如下:

 silverlight中使用Bing搜索_第1张图片

 

 

你可能感兴趣的:(silverlight)