首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows Phone,在异步回调上设置图像源不起作用?

Windows Phone,在异步回调上设置图像源不起作用?
EN

Stack Overflow用户
提问于 2012-08-08 06:27:11
回答 2查看 469关注 0票数 1

我正在从必应获取图片,以显示在我的应用程序中。我按照Bing的指示,成功地检索了图像的URL,但由于某些原因,仿真器不会显示它们!这是我的资料

代码语言:javascript
复制
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);

然后,我得到我的图像,并尝试在这里设置它们:

代码语言:javascript
复制
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]));

当我调试时,进程似乎只是在我设置源代码的最后三行停止。

EN

回答 2

Stack Overflow用户

发布于 2012-08-09 22:55:58

好吧,在经历了两天的挫折之后,我发现你不能从异步回调访问UI线程。VS没有给出任何错误,但图像没有显示。异步回调与主UI线程一起运行,因此它不能访问或更改UI中的元素。简单的解决方法只需要包装访问UI的代码行,如下所示:

代码语言:javascript
复制
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])); 
        });

它现在起作用了!

票数 1
EN

Stack Overflow用户

发布于 2012-08-08 13:29:32

您确定MediaUrl向图像返回了正确的urls吗?如果你对imList列表中的图片使用一些硬编码的urls,这些图片能在image1、image2和image3中正确加载吗?我要说的是,也许数据的质量是不正确的。也就是说,尽管您的查询执行得很好,但MediaURL没有包含格式正确的URL。

另外,当调试器停止时,您会得到什么异常?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11855015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档