C# 下载windows phone网站的内容
我正在尝试下载一个网站的html内容。出于某种原因,此代码不起作用。我希望windows phone 7和windows phone 8使用相同的东西。C# 下载windows phone网站的内容,c#,windows-phone-7,C#,Windows Phone 7,我正在尝试下载一个网站的html内容。出于某种原因,此代码不起作用。我希望windows phone 7和windows phone 8使用相同的东西。 谢谢。您的代码对我来说运行正常。您可能遇到了一个错误。为什么不检查一下是否收到任何错误 public MainPage() { InitializeComponent(); // Sample code to localize the ApplicationBar //Bu
谢谢。您的代码对我来说运行正常。您可能遇到了一个错误。为什么不检查一下是否收到任何错误
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));
}
}
如果出现错误会发生什么?你在回拨中忽略了这一点。还有,你得到了什么错误?您是否在回调中设置了断点?
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));
}
}