4

Bing Search API を使用して、アプリ内のタイルの背景として画像を検索しようとしています。プロジェクトに BingSearchContainer.cs を含めましたが、ここで提供されているサンプル コードでは機能しません。

私の Windows Phone 8 アプリ内で Bing Search API を使用する方法に関するガイドラインは、高く評価されます!

回答ありがとうございます。

4

1 に答える 1

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

  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 に答える