private void button7_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://asd.com/bb"));
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(e.Result);
}
else {
MessageBox.Show("err: " + e.Error.ToString());
}
}
如何从DownloadStringCompleted获取网址?或者我如何将一些参数传递给我的DownloadStringCompleted?
请帮助
答案 0 :(得分:5)
您可以通过DownloadStringAsync的第二个参数传递任何对象。然后,您可以通过DownloadStringCompletedEventArgs。UserState。
检索该对象private void button7_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
var uri = new Uri("http://asd.com/bb");
client.DownloadStringAsync(uri, uri);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var uri = e.UserState as Uri;
//...
}