我正在从必应获取图片,以显示在我的应用程序中。我按照Bing的指示,成功地检索了图像的URL,但由于某些原因,仿真器不会显示它们!这是我的资料
var bingContainer = new Bing.BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/"));
var accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);
var imageQuery = bingContainer.Image("porsche", null, null, null, null, null, "Size:Medium");
imageQuery.BeginExecute(new AsyncCallback(this.ImageResultLoadedCallback), imageQuery);然后,我得到我的图像,并尝试在这里设置它们:
var imageQuery = (DataServiceQuery<Bing.ImageResult>)ar.AsyncState;
var enumerableImages = imageQuery.EndExecute(ar);
var imagesList = enumerableImages.ToList();
List<String> imList = new List<String>();
while (imList.Count != 3)
{
Bing.ImageResult tr = imagesList.First<Bing.ImageResult>();
if (tr.ContentType == "image/jpeg")
{
imList.Add(tr.MediaUrl);
}
imagesList.RemoveAt(0);
}
image1.Source = new BitmapImage(new Uri(@imList[0]));
image2.Source = new BitmapImage(new Uri(@imList[1]));
image3.Source = new BitmapImage(new Uri(@imList[2]));当我调试时,进程似乎只是在我设置源代码的最后三行停止。
发布于 2012-08-09 22:55:58
好吧,在经历了两天的挫折之后,我发现你不能从异步回调访问UI线程。VS没有给出任何错误,但图像没有显示。异步回调与主UI线程一起运行,因此它不能访问或更改UI中的元素。简单的解决方法只需要包装访问UI的代码行,如下所示:
Dispatcher.BeginInvoke(() =>
{
image1.Source = new BitmapImage(new Uri(@imList[0]));
image2.Source = new BitmapImage(new Uri(@imList[1]));
image3.Source = new BitmapImage(new Uri(@imList[2]));
});它现在起作用了!
发布于 2012-08-08 13:29:32
您确定MediaUrl向图像返回了正确的urls吗?如果你对imList列表中的图片使用一些硬编码的urls,这些图片能在image1、image2和image3中正确加载吗?我要说的是,也许数据的质量是不正确的。也就是说,尽管您的查询执行得很好,但MediaURL没有包含格式正确的URL。
另外,当调试器停止时,您会得到什么异常?
https://stackoverflow.com/questions/11855015
复制相似问题