4

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

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

感谢您的任何回答。

4

1 回答 1

7

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

执行

  1. 首先,将BingSearchContainer.cs添加到您的项目中
  2. 实现Bing API 快速入门和代码中的示例 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";
                });
            }
        }
    }
    
于 2013-02-27T20:45:37.410 回答