如何在Windows Phone中使用Bing Search API?

Mik*_*der 4 c# bing-api windows-phone azure-marketplace windows-phone-8

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

有关如何在我的Windows Phone 8应用程序中使用Bing Search API的任何指导原则都会受到关注!

谢谢你的回答.

Mik*_*der 7

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

履行

  1. 首先,将BingSearchContainer.cs添加到您的项目中
  2. 实现Bing API快速入门和代码中的示例C#代码
  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);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

  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";
                });
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)