Bing API查询回溯到Observable Collection

时间:2014-11-21 05:54:40

标签: c# xaml gridview windows-store-apps

使用此帖子中的一些信息创建了Windows应用商店应用:How do I use the Bing Search API in Windows Phone?

目标

文本框 - 键入任何术语 搜索按钮 - 搜索该术语并填充使用Bing API检索的图片的GridView

问题

我收到了照片,并通过我的" OnQueryComplete"回调,但我无法弄清楚填充集合的正确方法是什么。由于我无法弄清楚如何等待这个调用,我(只是为了看看我是否可以使它工作,它确实如此)添加了一个while循环(你可能会看到它的问题)。这样做的正确方法是什么?你如何处理填充GridView的回调并让它等到它完成?

当前ViewModel代码

    public bool itemsFinished = false;
    private ObservableCollection<SearchResult> _ImageResults;
    public ObservableCollection<SearchResult> ImageResults {
        get {
            if (_ImageResults == null) {
                while (!itemsFinished) {
                    int i = 0;
                    i++;
                }
            }
            return _ImageResults;
        }
        set {
            _ImageResults = value;
        }
    }

    public SearchResultViewModel() {
       GetPictures("dogs");
    }


    public void GetPictures(string searchTerm) {
        // This is the query - or you could get it from args. 
        string query = searchTerm;
        // Create a Bing container. 
        string rootUri = "https://api.datamarket.azure.com/Bing/Search";
        var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri));
        // Replace this value with your account key. 
        var accountKey = "myaccountkey";
        // 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(OnQueryComplete, imageQuery);

        // var imageResults = imageQuery.Execute(); 
    }

    private void OnQueryComplete(IAsyncResult result) {
       // ImageResults.Clear();
        _ImageResults = new ObservableCollection<SearchResult>();
        var query = (DataServiceQuery<ImageResult>)result.AsyncState;
        var enumerableResults = query.EndExecute(result);
        int i = 0;
        foreach (var item in enumerableResults) {
            SearchResult myResult = new SearchResult();
            myResult.Title = item.Title;
            myResult.ImageUri = new Uri(item.MediaUrl);
            ImageResults.Add(myResult);
            i++;
            if (i >= 14) {
                break;
            }
        }
        itemsFinished = true;

    }

1 个答案:

答案 0 :(得分:0)

请原谅任何语法错误,我现在没有Visual Studio实例。

我看到的问题是,当您收到内容时,您会重置ObservableCollection

尝试如下:

private ObservableCollection<SearchResult> _ImageResults;
public ObservableCollection<SearchResult> ImageResults {
    get
    {
        return _ImageResults;
    }
    set {
        _ImageResults = value;
    }
}

public SearchResultViewModel() {
   _ImageResults = new ObservableCollection<SearchResult>(); // Just create it once.
   GetPictures("dogs");
}

private void OnQueryComplete(IAsyncResult result) {
    _ImageResults.Clear(); // Clear isn't bad, that way you keep your reference to your original collection!
    //_ImageResults = new ObservableCollection<SearchResult>(); // We already have one. ObservableCollection works best if you keep on working with the collection you have.
    var query = (DataServiceQuery<ImageResult>)result.AsyncState;
    var enumerableResults = query.EndExecute(result);
    int i = 0;
    foreach (var item in enumerableResults) {
        SearchResult myResult = new SearchResult();
        myResult.Title = item.Title;
        myResult.ImageUri = new Uri(item.MediaUrl);
        ImageResults.Add(myResult);
        i++;
        if (i >= 14) {
            break;
        }
    }
}

据我所知(不能悲伤地测试),如果您在xaml中绑定了ObservableCollection正确的方法,这应该可行。