WPF异步httpclient没有返回字符串

时间:2017-01-26 13:51:43

标签: c# xml wpf rss httpclient

public RSS_Reader()
{
    this.InitializeComponent();
}

public static async Task<string> DownloadPageAsync(string pageURL)
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("http://www.parliament.uk/g/RSS/news-feed/?pageInstanceId=209&limit=20");
    HttpContent content = response.Content;
    string result = await content.ReadAsStringAsync();
    return result;

}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var parameter = e.Parameter as string;
    strURL = parameter.ToString();

    Task<string> strXML = DownloadPageAsync(strURL);

    ListBoxRss.Items.Add(strXML.Result);
 }

我一直在制作的wp8应用程序的一部分。应用程序的主要登录页面链接到我从上面的代码中获取的第二页。第二页从未实际加载,只是挂在第一页上。

我做错了什么? 感谢。

1 个答案:

答案 0 :(得分:1)

您应该使OnNavigatedTo方法异步并等待DownloadPageAsync方法:

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    var parameter = e.Parameter as string;
    strURL = parameter.ToString();

    string strXML = await DownloadPageAsync(strURL);

    ListBoxRss.Items.Add(strXML);
}