How to use Bing Search API in Windows Phone?

I am trying to use the Bing Search API to search for images as a background for tiles inside my application. I have included BingSearchContainer.cs in my project, but I cannot get it to work with the sample code given here.

All recommendations for using the Bing Search API inside my application for Windows Phone 8 will be published!

Thanks for any answer.

+4
source share
1 answer

I expect that you already have an AccountKey, so I will not say that you need to get it.

Implementation

  • First of all, add BingSearchContainer.cs to your project
  • Implement C # sample code found in Quick Launch and Bing API Code
  • Then right-click Links and select Manage NuGet Packages ... and search and install Microsoft.Data.Services.Client.WindowsP .
  • Modify the sample code to work with Windows Phone:

     using Bing; using System; using System.Data.Services.Client; using System.Linq; using System.Net; namespace StackOverflow.Samples.BingSearch { public class Finder { public void FindImageUrlsFor(string searchQuery) { // Create a Bing container. string rootUri = "https://api.datamarket.azure.com/Bing/Search"; var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri)); bingContainer.UseDefaultCredentials = false; // Replace this value with your account key. var accountKey = "YourAccountKey"; // Configure bingContainer to use your credentials. bingContainer.Credentials = new NetworkCredential(accountKey, accountKey); // Build the query. var imageQuery = bingContainer.Image(query, null, null, null, null, null, null); imageQuery.BeginExecute(_onImageQueryComplete, imageQuery); } // Handle the query callback. private void _onImageQueryComplete(IAsyncResult imageResults) { // Get the original query from the imageResults. DataServiceQuery<Bing.ImageResult> query = imageResults.AsyncState as DataServiceQuery<Bing.ImageResult>; var resultList = new List<string>(); foreach (var result in query.EndExecute(imageResults)) resultList.Add(result.MediaUrl); FindImageCompleted(this, resultList); } public event FindImageUrlsForEventHandler FindImageUrlsForCompleted; public delegate void FindImageUrlsForEventHandler(object sender, List<string> result); } } 

Example

  • And now, let me use the code that I have provided you:

     using Bing; using System; using System.Data.Services.Client; using System.Linq; using System.Net; namespace StackOverflow.Samples.BingSearch { public class MyPage { private void Button_Click_1(object sender, RoutedEventArgs e) { var finder = new Finder(); finder.FindImageUrlsForCompleted += finder_FindImageUrlsForCompleted; finder.FindImageUrlsFor("candy"); } void finder_FindImageUrlsForCompleted(object sender, List<string> result) { Deployment.Current.Dispatcher.BeginInvoke(() => { foreach (var s in result) MyTextBox.Text += s + "\n"; }); } } } 
+7
source

All Articles