【问题标题】:How do I use the Bing Search API in Windows Phone?如何在 Windows Phone 中使用必应搜索 API?
【发布时间】:2013-02-27 20:45:37
【问题描述】:

我正在尝试使用 Bing 搜索 API 来查找图像作为我的应用程序内部图块的背景。我已将 BingSearchContainer.cs 包含在我的项目中,但无法使用此处提供的示例代码使其工作。

任何关于如何在我的 Windows Phone 8 应用程序中使用 Bing 搜索 API 的指南都会被应用!

感谢您的任何回答。

【问题讨论】:

    标签: c# windows-phone-8 windows-phone bing-api azure-marketplace


    【解决方案1】:

    我希望您已经拥有一个 AccountKey,所以我不会告诉您必须拥有一个。

    实施

    1. 首先,将BingSearchContainer.cs 添加到您的项目中
    2. 实现Bing API Quick Start & Code 中的示例 C# 代码
    3. 然后,右键单击 References 并选择 Manage NuGet Packages...,然后搜索并安装 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";
                  });
              }
          }
      }
      

    【讨论】:

    • 我想在 iOS 应用程序中集成相同的 api。你能告诉我我们应该拥有哪个帐户吗?我们可以在哪里注册?谢谢你。
    • 我想补充一点,您应该设置 bingContainer.UseDefaultCredentials = false ,如 social.msdn.microsoft.com/Forums/windowsazure/en-US/… 中所述。
    • 我现在已经实现了,@redtuna
    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多