在RequestEnd上填充ListBox

时间:2013-07-03 13:57:44

标签: windows-phone-7 asynchronous windows-phone-8 httprequest httpwebresponse

使用Windows Phone 8。

遇到了几年前我以某种方式解决过的问题,但不记得了。

GetNews();
InitializeComponent();

private void GetNews()
{
    HttpWebRequest request = HttpWebRequest.CreateHttp(GS.BACKEND_HOST + "something");
    request.BeginGetResponse(new AsyncCallback(HandleResponseNews), request);
}

private void HandleResponseNews(IAsyncResult result)
{
    HttpWebRequest request = result.AsyncState as HttpWebRequest;
    if (request != null)
    {
        using (WebResponse response = request.EndGetResponse(result))
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string JSON = reader.ReadToEnd();
                NewsList = JsonConvert.DeserializeObject<News>(JSON) as News;
            }
        }
    }

}

我需要用新闻列表项填充我的列表框。但是,只有在NewsList充满了请求响应之后我才能做到。

我需要某种类型的触发器......

1 个答案:

答案 0 :(得分:-1)

找到解决方案:

 Deployment.Current.Dispatcher.BeginInvoke(() => 
 { 
      foreach (Article article in NewsList.Result.Articles) 
      { 
      NewsListBox.Items.Add(article.Title); 
      } 
  });