我如何使用Windows Phone的Bing搜索API? [英] How do I use the Bing Search API in Windows Phone?

查看:248
本文介绍了我如何使用Windows Phone的Bing搜索API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Bing搜索API找到图像作为背景,我的应用程序内的瓷砖。我包括我的项目中BingSearchContainer.cs但我不能使它与这里提供的示例代码。



有关如何使用Bing搜索的任何指引我的Windows Phone 8应用程序的内部API将appriciated!



感谢您的任何答复。


解决方案

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



实施




  1. 首先,添加的 BingSearchContainer.cs 您的项目

  2. 实施中的兵API快速入门和放大器;代码

  3. 之后,右键点击参考并选择管理的NuGet包... 并搜索和安装, Microsoft.Data.Services.Client.WindowsP

  4. 修改示例代码,使其与Windows Phone的工作:

     使用Bing; 
    使用系统;
    使用System.Data.Services.Client;
    使用System.Linq的;使用System.Net
    ;

    命名空间StackOverflow.Samples.BingSearch
    {
    公共类搜索
    {
    公共无效FindImageUrlsFor(字符串SEARCHQUERY)
    {
    //创建一个兵容器。
    串rootUri =htt​​ps://api.datamarket.azure.com/Bing/Search;
    变种bingContainer =新Bing.BingSearchContainer(新的URI(rootUri));
    bingContainer.UseDefaultCredentials = FALSE;

    //替换您的账号密码此值。
    VAR accountKey =YourAccountKey;

    //配置bingContainer使用您的凭据。
    bingContainer.Credentials =新的NetworkCredential(accountKey,accountKey);

    //构建查询。
    VAR imageQuery = bingContainer.Image(查询,NULL,NULL,NULL,NULL,NULL,NULL);

    imageQuery.BeginExecute(_onImageQueryComplete,imageQuery);

    }

    //处理查询回调。
    私人无效_onImageQueryComplete(IAsyncResult的imageResults)
    {
    //从imageResults原始查询。
    &化DataServiceQuery LT; Bing.ImageResult>查询=
    imageResults.AsyncState作为化DataServiceQuery< Bing.ImageResult取代;

    变种resultList =新的List<串GT;();

    的foreach(VAR结果query.EndExecute(imageResults))
    resultList.Add(result.MediaUrl);

    FindImageCompleted(这一点,resultList);
    }

    公共事件FindImageUrlsForEventHandler FindImageUrlsForCompleted;
    公众委托无效FindImageUrlsForEventHandler(对象发件人,列表与LT;字符串>结果);
    }
    }




示例




  1. 现在,让我们使用我提供你的代码:

     使用Bing; 
    使用系统;
    使用System.Data.Services.Client;
    使用System.Linq的;使用System.Net
    ;

    命名空间StackOverflow.Samples.BingSearch
    {
    公共类我的页面
    {
    私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
    {
    变种取景器=新的Finder();
    finder.FindImageUrlsForCompleted + = finder_FindImageUrlsForCompleted;
    finder.FindImageUrlsFor(糖);
    }

    无效finder_FindImageUrlsForCompleted(对象发件人,列表与LT;字符串>结果)
    {
    Deployment.Current.Dispatcher.BeginInvoke(()=>
    {
    的foreach(在结果变种S)
    MyTextBox.Text + = S +\\\
    ;
    });
    }
    }
    }



I'm trying to use the Bing Search API to find images as backgrounds to the tiles inside of my app. I've included the BingSearchContainer.cs in my Project but I can't make it work with the sample code provided here.

Any guidelines for how to use the Bing Search API inside of my Windows Phone 8 app would be appriciated!

Thanks for any answer.

解决方案

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

Implementation

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

Example

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

这篇关于我如何使用Windows Phone的Bing搜索API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆