-->

如何使用Windows Phone的Bing搜索API?(How do I use the Bing

2019-07-20 15:55发布

我试图使用Bing搜索API来找到我的应用程序的内部图像作为背景的瓷砖。 我包括我的项目中BingSearchContainer.cs但我不能使它与这里提供的示例代码。

对于如何使用Bing搜索API我的Windows Phone 8应用程序内部的任何准则将被appriciated!

感谢您的任何答复。

Answer 1:

我希望你已经有了一个AccountKey所以我不会告诉你必须得到一个。

履行

  1. 首先,在加BingSearchContainer.cs到项目
  2. 实现在发现C#示例代码兵API快速入门和代码
  3. 此后,右键单击引用 ,并选择管理的NuGet包...和搜索,并安装, Microsoft.Data.Services.Client.WindowsP
  4. 修改示例代码,使其与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); } } 

  1. 现在,让我们使用我提供你的代码:

     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"; }); } } } 


文章来源: How do I use the Bing Search API in Windows Phone?