0

我找不到如何从此代码中替换 Webclient。

什么是新的命名空间以及在 Windows 8 中创建它的方式...... Win 8 dev 的新功能。

任何提示或帖子都会有所帮助,或者下面的任何代码。

WebClient client = new WebClient();
 client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
 Uri url2 = new Uri("http://www.buscms.com/api/XmlEntities/v1/stops.aspx", UriKind.Absolute);
 client.DownloadStringAsync(url2);


 void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    if (e.Error == null) 
    { 
        if (e.Result != null) 
        { 
            XDocument doc = XDocument.Parse(e.Result); 
            XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary"; 
            var routeNames = (from n in doc.Descendants(ns + "ArrayOfStop") 
                             select new RootContainer2
                             { 
                                 Departures = (from s in n.Elements(ns + "Stop") 
                                          select new Departures
                                          {

                                              StopName = s.Element(ns + "StopName").Value,    

                                          }).ToList()
                             }).Single();

            listBox2.ItemsSource = routeNames.Departures;





      } 
    } 
} 
4

1 回答 1

2

您可以使用 HTTPClient 代替 WebClient,如下所示:

public async Task<string> DownloadTheString()
{
    HttpClient http = new System.Net.Http.HttpClient();
    HttpResponseMessage response = await http.GetAsync("http://www.buscms.com/api/XmlEntities/v1/stops.aspx");
    string result = await response.Content.ReadAsStringAsync();

    // Return the downloaded string
    return result;
}

这也是另一个非常相似的问题:Windows 8 中不存在 WebClient 类

于 2012-12-17T22:47:46.600 回答