Building Xamarin Mobile Application with Bing AutoSuggest using Cognitive Services
The Bing Autosuggest API returns a list of suggested queries based on the user enters in the search box. Display the suggestions in the search box's drop-down list. The suggested terms are based on suggested queries that other users have searched on and user intent.
In this article, I will show how to generate Bing Search subscription key and integrate into the Xamarin application
Register Bing Search in Azure Portal:
You need to create an Azure account and generateStep 1:
Create On “+ Create a resource “> Under Azure Marketplace, select AI + Cognitive Services
and discover the list of available APIs. > Select “ Bing Search v7 APIs”
Step 3:
Step 4:
You can copy the Endpoint URL in the Overview section and keys in the Keys section to start making API calls in our Xamarin applications.
Create Xamarin Application with Bing AutoSuggest :
Let's start with creating a new Xamarin Forms Project using Visual Studio. Open RunType “Devenev
It will automatically create multiple projects, like .NET Standard, Android, iOS, and UWP.
Install Newtonsoft. Json :
Bing Autosuggest will return Json object value so make sure you have added the Newtonsoft JSON NuGet Package to your all
Install Newtonsoft Json
Install Microsoft. Csharp :
This Design View:
After successfully install above two<
<
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
<
<Entry x
TextChanged="OnTextChangesEvent" />
<ListView x:Name="lstautosug" BackgroundColor="Azure"
SelectedItem="{Binding Source={x:Reference entrysearch},
Path=Text}"></ListView>
</StackLayout>
</ContentPage>
Configure the project:
Open the MainPage.xaml.cs file from PCL project > replace your subscription key and End urlprivate string AutoSuggestionEndPoint = "https://api.cognitive.microsoft.com/
bing/v7.0/suggestions";
public HttpClient AutoSuggestionClient
{
get;
set;
}
public MainPage()
{
InitializeComponent();
AutoSuggestionClient = new HttpClient();
AutoSuggestionClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key",
"<Key>");
}
Get and Parse Json Data:
HttpClient class provides a base class for get/Post the HTTP requests/responses from a URL.It is a supported async feature of .NET framework. HttpClient is able to process multiple concurrent requests.
The following code showing get all Json data using Bing suggestions API url and Parse the json and binding into the list view for autocomplete
private async void OnTextChangesEvent(object sender, TextChangedEventArgs e)
{
try
{
if(entrysearch!=null)
lstautosug.ItemsSource = await GetAutoSuggestResults(this.entrysearch.Text);
}
catch (HttpRequestException)
{
}
}
public async Task<List<string>> GetAutoSuggestResults(string query)
{
List<string> suggestions = new List<string>();
string market = "en-US";
var result = await AutoSuggestionClient.GetAsync(string.Format("{0}/
?q={1}&mkt={2}", AutoSuggestionEndPoint,
WebUtility.UrlEncode(query), market));
result.EnsureSuccessStatusCode();
var json = await result.Content.ReadAsStringAsync();
dynamic data = JObject.Parse(json);
if (data.suggestionGroups != null && data.suggestionGroups.Count >
0 && data.suggestionGroups[0].
searchSuggestions != null)
{
for (int i = 0; i < data.suggestionGroups[0].searchSuggestions.Count; i++)
{
suggestions.Add(data.suggestionGroups[0].searchSuggestions[i].displayText.Value);
}
}
return suggestions;
}
Run the Application:
We have completed the coding now start run the application so you can select the platform iOS,Android , Windows and Click on Run (f5) the application .You can find the source code at C# Corner attachment and Github XamarinBingAutoSuggest repository as Microsoft-Cognitive-Service.
This comment has been removed by the author.
ReplyDeleteDo you have a spam issue on this website; I also am a blogger, and I wanted to know your situation; many of us have developed some nice methods, and we are looking to trade methods with others, why not shoot me an e-mail if interested.Surya Informatics
ReplyDeleteNice blog..i was really impressed by seeing this blog, it was very interesting and it is very useful for me.also the information which you have mentioned here is correct and impressive. Really appreciate.
ReplyDeleteHire Xamarin Developer
Xamarin Development Company
Nice blog..i was really impressed by seeing this blog, it was very interesting and it is very useful for me.also the information which you have mentioned here is correct and impressive. Really appreciate.
ReplyDeleteHire Xamarin Developer
Xamarin Development Company
Your blog is very interesting. Your level of thinking is good and the clarity of writing is excellent. I enjoyed so much to read this post. I am also a blogger, you can follow my blog here iOS App Development Company
ReplyDelete