-2
       public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }




    private void b1_Click(object sender, RoutedEventArgs e)
    {
        string url = trackingtb.Text;
        LoadSiteContent("http://www.mywebsite.com");

    }

    public void LoadSiteContent(string url)
    {
        //create a new WebClient object
        WebClient client = new WebClient();

        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2);
        client.DownloadStringAsync(new Uri(url));
    }

    private  void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            output.Text= (string)e.Result;
            //If you get the cross-thread exception then use the following line instead of the above
            //Dispatcher.BeginInvoke(new Action (() => textBlock1.Text = (string)e.Result));
        }
    }

我正在尝试下载网站的 html 内容。由于某种原因,此代码不起作用。我希望 windows phone 7 和 windows phone 8 使用相同的东西。
谢谢。

4

1 回答 1

0

Your code works correctly for me. You are probably gettin an error. Why don't you check if you are receiving any errors?

private  void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // Check the error here
    }
    // If the request was not canceled and did not throw
    // an exception, display the resource.
    else if (!e.Cancelled)
    {
        output.Text= (string)e.Result;
        //If you get the cross-thread exception then use the following line instead of the above
        //Dispatcher.BeginInvoke(new Action (() => textBlock1.Text = (string)e.Result));
    }
}
于 2013-06-18T13:11:25.020 回答