下载网站windows手机的内容

时间:2013-06-17 23:32:51

标签: c# windows-phone-7

       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使用相同的东西 感谢。

1 个答案:

答案 0 :(得分:0)

您的代码可以正常使用。你可能会收到一个错误。你为什么不检查你是否收到任何错误?

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));
    }
}