Windows Phone 7 DownloadStringCompleted和url是什么?还是参考?

时间:2011-10-01 13:24:27

标签: c# windows-phone-7

    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?

请帮助

1 个答案:

答案 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;
    //...
}