相关疑难解决方法(0)

将WebClient方法转换为async/await

我有一些现有的代码,我移植到Windows 8 WinRT.代码从URL获取数据,异步调用传递的委托:

private void RequestData(string uri, Action<string> action)
{
  var client = new WebClient();
  client.DownloadStringCompleted += (s,e) => action(e.Result);
  client.DownloadStringAsync(new Uri(uri));
}
Run Code Online (Sandbox Code Playgroud)

转换为WinRT需要使用HttpClient和异步方法.我已经阅读了一些关于async/await的教程,但有点困惑.如何更改上述方法,但保留方法签名以避免更改我的代码?

c# async-await microsoft-metro

35
推荐指数
4
解决办法
4万
查看次数

如何在WebClient.DownloadStringAsync URL中使用String

我目前有这个用于我的WebClient URL

WebClient Detail = new WebClient();
Detail.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + ListingID.Text + ".xml"));
Run Code Online (Sandbox Code Playgroud)

我想要做的是使用这个字符串:

void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs args)
{
    var lbi = ((sender as ListBox).SelectedItem as TradeItem);
    if(lbi != null)
    {
        string id = lbi.ListingId.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

作为WbeClient URL的一部分.

例:

WebClient Detail = new WebClient();
Detail.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Detail_DownloadStringCompleted);
Detail.DownloadStringAsync(new Uri("http://api.trademe.co.nz/v1/Listings/" + id + ".xml"));
Run Code Online (Sandbox Code Playgroud)

无论如何在URL中使用此字符串如上所示?

完整代码#####################################

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media; …
Run Code Online (Sandbox Code Playgroud)

c# silverlight webclient windows-phone-7

13
推荐指数
3
解决办法
4万
查看次数