且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Windows Phone 中使用必应搜索 API?

更新时间:2023-11-10 15:15:52

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

I expect that you already have a AccountKey so I wont tell you have to get one.

  1. 首先,添加BingSearchContainer.cs 到您的项目
  2. 实现 Bing API 快速入门 & 中的示例 C# 代码代码
  3. 然后,右键单击引用并选择管理 NuGet 包...,然后搜索并安装 Microsoft.Data.Services.Client.WindowsP.
  4. 修改示例代码,使其适用于 Windows Phone:

  1. First of all, add the BingSearchContainer.cs to your project
  2. Implement the sample C# code found in the Bing API Quick Start & Code
  3. Thereafter, right-click References and choose Manage NuGet Packages... and search for, and install, Microsoft.Data.Services.Client.WindowsP.
  4. Modify the sample code so that it work with Windows Phone:

using Bing;
using System;
using System.Data.Services.Client;
using System.Linq;
using System.Net;

namespace ***.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. 现在,让我们使用我提供给您的代码:

  1. And now, let's use the code I provided you with:

using Bing;
using System;
using System.Data.Services.Client;
using System.Linq;
using System.Net;

namespace ***.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 + "
";
            });
        }
    }
}