Many large websites provide external interfaces, such as Bing, so that we can use Bing APIs to use the search function.
First, we need a bing appid to use Bing's API. At http://www.bing.com/developers/createapp.aspx, we can use Bing appid.
Add service references: http://api.search.live.net/search.wsdl? To the Silverlight Project? Appid = ******.
XAML:
Code
< 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
Code
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 ();
}
}
The result is as follows: