【问题标题】:WPF async httpclient not returning stringWPF异步httpclient不返回字符串
【发布时间】:2017-01-26 13:51:42
【问题描述】:
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 应用程序的一部分。该应用程序的主登录页面链接到我从上面获取代码的第二页。第二页实际上从未加载,它只是挂在第一页上。

我做错了什么? 谢谢。

【问题讨论】:

    标签: c# xml wpf rss httpclient


    【解决方案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);
    }
    

    【讨论】:

    • 耶耶,我要读完议会集锦了!
    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    相关资源
    最近更新 更多