我正在尝试使用必应搜索API来查找图片作为我的应用程序内的磁贴的背景。我已经在我的项目中包含了BingSearchContainer.cs,但是我不能使用这里提供的示例代码使它工作。
任何关于如何在我的Windows Phone 8应用程序中使用Bing搜索API的指南都会得到重视!
谢谢你的回复。
发布于 2013-02-28 04:45:37
我希望你已经有了一个AccountKey,所以我不会告诉你必须要有一个。
实现
首先,在您的project
Microsoft.Data.Services.Client.WindowsP
.
使用Bing;使用System;使用System.Data.Services.Client;使用System.Linq;使用System.Net;命名空间StackOverflow.Samples.BingSearch { public class Finder { public void FindImageUrlsFor(string searchQuery) { //创建一个Bing容器。string rootUri = "https://api.datamarket.azure.com/Bing/Search";var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri));bingContainer.UseDefaultCredentials = false;//将该值替换为您的账户密钥。var accountKey = "YourAccountKey";//配置bingContainer使用您的凭证。bingContainer.Credentials = new NetworkCredential(accountKey,accountKey);//构建查询imageQuery.BeginExecute(_onImageQueryComplete,imageQuery = bingContainer.Image( query,null,null);var imageQuery);} //处理查询回调private void _onImageQueryComplete(IAsyncResult imageResults) { //从imageResults获取原始查询DataServiceQuery查询= imageResults.AsyncState as DataServiceQuery;var resultList =新列表();foreach (query.EndExecute(ImageResults)中的var结果) resultList.Add(result.MediaUrl);FindImageCompleted(this,resultList);}公共事件FindImageUrlsForEventHandler FindImageUrlsForCompleted;公共委托void FindImageUrlsForEventHandler(对象发送者,列表结果);} }
示例
使用Bing;使用系统;使用System.Data.Services.Client;使用System.Linq;使用System.Net;命名空间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(“finder.FindImageUrlsForCompleted”);} void finder_FindImageUrlsForCompleted(对象发送者,列表结果){ Deployment.Current.Dispatcher.BeginInvoke(() => { foreach (var S in result) MyTextBox.Text += s+ "\n";});}
https://stackoverflow.com/questions/15122037
复制相似问题