Bing Service Into UWP App Using UWP Community Toolkit

Introduction

This article is divided into two sections. The first part explains about the installation of UWP Service Tool kit while the second part explains about  integrating Bing service into the application.

  1. Install the UWP Service
  2. Integrate Bing Service into main application

Install the UWP Toolkit

UWP Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps.

More info available here.

Adding helper reference into our project.

Steps

  1. Create a new project (ex: Toolkit)
  2. Right click -> Manage NuGet Packages
  3. Search “Microsoft.Toolkit.Uwp”
  4. Select the “Microsoft.Toolkit.Uwp.Services” & Install
 
Bing Service - Below are major classes and functions used to get the Bing Search records
  1. BingSearchConfig
  2. BingService
  3. BingService.Instance.RequestAsync  
BingSearchConfig class is used to configure the query , BingService class is used to connect to the Bing server, BingService.Instance.RequestAsync function is used to request to the Bing server to get the searched data. 
  1. var searchConfig = new BingSearchConfig  
  2. {  
  3.       Query = "Vinoth",  
  4.       Country = BingCountry.India  
  5. };  
  6. BingService.Instance.RequestAsync(searchConfig, 30);    
Note - Country property is used to query search based on the country & Bing supported country list is available in BingCountry Enum. We will see a simple example, and search the same keywords at a time in two different countries.

Code sample
 
  1. //To Load Bing supported country list  
  2.         private void LoadCountry()  
  3.         {  
  4.             var lstCountry = new List<string>();  
  5.             for (int i = (int)BingCountry.UnitedArabEmirates; i < (int) BingCountry.SouthAfrica; i++)  
  6.             {  
  7.                 var item = (BingCountry) i;  
  8.                 lstCountry.Add(item.ToString());  
  9.             }  
  10.   
  11.             Cblang.ItemsSource = lstCountry;  
  12.             Cblang.SelectedIndex = 0;  
  13.         }   
  1. //Search query  
  2.         private  async Task<List<BingResult>> LoadSearch(string searchText,string selCountry)  
  3.         {  
  4.             if (Cblang.SelectedItem != null)  
  5.             {  
  6.                 var searchConfig = new BingSearchConfig  
  7.                 {  
  8.                     Query = searchText  
  9.                 };  
  10.                 BingCountry bingCountry = (BingCountry)Enum.Parse(typeof(BingCountry), selCountry, true);  
  11.                 searchConfig.Country = bingCountry;  
  12.                  
  13.                 return await BingService.Instance.RequestAsync(searchConfig, 30);                
  14.             }  
  15.             return null;  
  16.         }   
  1. private async void BtnSearch_OnClick(object sender, RoutedEventArgs e)  
  2.        {  
  3.            if (Cblang.SelectedItem != null)  
  4.            {  
  5.                var selCountry = Cblang.SelectedItem.ToString();  
  6.                //Country Based Search  
  7.                ListView.ItemsSource = await LoadSearch(TxtSearch.Text, selCountry);  
  8.                //Default search query based on India  
  9.                ListView1.ItemsSource = await LoadSearch(TxtSearch.Text, "India");  
  10.            }  
  11.        }  
Xaml Code
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.           
  3.         <Grid.RowDefinitions>  
  4.             <RowDefinition Height="Auto"/>  
  5.             <RowDefinition Height="85*"/>  
  6.         </Grid.RowDefinitions>  
  7.           
  8.         <StackPanel>  
  9.             <StackPanel>  
  10.                 <TextBlock Text="Search"/>  
  11.                 <TextBox x:Name="TxtSearch" PlaceholderText="Type keyword and Start Search"/>  
  12.             </StackPanel>  
  13.             <StackPanel Orientation="Horizontal">  
  14.                 <TextBlock Text="Language"/>  
  15.                 <ComboBox x:Name="Cblang"></ComboBox>  
  16.                 <Button x:Name="BtnSearch" Content="Search" Click="BtnSearch_OnClick"></Button>  
  17.             </StackPanel>  
  18.         </StackPanel>  
  19.   
  20.         <Grid Grid.Row="1">  
  21.             <Grid.ColumnDefinitions>  
  22.                 <ColumnDefinition/>  
  23.                 <ColumnDefinition/>  
  24.             </Grid.ColumnDefinitions>  
  25.   
  26.             <ListView x:Name="ListView" Grid.Column="0">  
  27.                 <ListView.ItemTemplate>  
  28.                     <DataTemplate x:DataType="bing:BingResult">  
  29.                         <Grid Margin="0,5,10,5">  
  30.                             <Grid.ColumnDefinitions>  
  31.                                 <ColumnDefinition></ColumnDefinition>  
  32.                                 <ColumnDefinition Width="Auto"></ColumnDefinition>  
  33.                             </Grid.ColumnDefinitions>  
  34.                             <Grid.RowDefinitions>  
  35.                                 <RowDefinition></RowDefinition>  
  36.                                 <RowDefinition></RowDefinition>  
  37.                                 <RowDefinition></RowDefinition>  
  38.                             </Grid.RowDefinitions>  
  39.   
  40.                             <TextBlock Text="{x:Bind Title}" Grid.Row="0" Grid.Column="0" FontWeight="Bold" TextTrimming="CharacterEllipsis"></TextBlock>  
  41.   
  42.                             <TextBlock Text="{x:Bind Published}" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right"/>  
  43.   
  44.                             <TextBlock Text="{x:Bind Summary}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" TextWrapping="Wrap"></TextBlock>  
  45.   
  46.                             <HyperlinkButton Content="{x:Bind Link}" NavigateUri="{x:Bind Link}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"></HyperlinkButton>  
  47.                         </Grid>  
  48.                     </DataTemplate>  
  49.                 </ListView.ItemTemplate>  
  50.             </ListView>  
  51.             <ListView x:Name="ListView1" Grid.Column="1">  
  52.                 <ListView.ItemTemplate>  
  53.                     <DataTemplate x:DataType="bing:BingResult">  
  54.                         <Grid Margin="0,5,10,5">  
  55.                             <Grid.ColumnDefinitions>  
  56.                                 <ColumnDefinition></ColumnDefinition>  
  57.                                 <ColumnDefinition Width="Auto"></ColumnDefinition>  
  58.                             </Grid.ColumnDefinitions>  
  59.                             <Grid.RowDefinitions>  
  60.                                 <RowDefinition></RowDefinition>  
  61.                                 <RowDefinition></RowDefinition>  
  62.                                 <RowDefinition></RowDefinition>  
  63.                             </Grid.RowDefinitions>  
  64.   
  65.                             <TextBlock Text="{x:Bind Title}" Grid.Row="0" Grid.Column="0" FontWeight="Bold" TextTrimming="CharacterEllipsis"></TextBlock>  
  66.   
  67.                             <TextBlock Text="{x:Bind Published}" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right"/>  
  68.   
  69.                             <TextBlock Text="{x:Bind Summary}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" TextWrapping="Wrap"></TextBlock>  
  70.   
  71.                             <HyperlinkButton Content="{x:Bind Link}" NavigateUri="{x:Bind Link}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"></HyperlinkButton>  
  72.                         </Grid>  
  73.                     </DataTemplate>  
  74.                 </ListView.ItemTemplate>  
  75.             </ListView>  
  76.         </Grid>  
  77.     </Grid>   
Output : We are searching the same keyword in two different countries and we get two different outputs based on the country.

 

Mobile

 

The above code is just an example. Hope you enjoyed and understood it.


Similar Articles