在网上搜索了大约 4 个小时后,我仍然不了解 Windows Phone 7 上的异步函数。我尝试运行该代码,但看起来我的 webClient 的“DownloadStringCompleted”事件从未引发过。我试图在这里等待答案,但它只是冻结了我的应用程序。任何人都可以帮助并解释为什么它不起作用?
internal string HTTPGet()
{
string data = null;
bool exit = false;
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
webClient.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
data = e.Result;
exit = true;
}
};
webClient.DownloadStringAsync(new Uri(site, UriKind.Absolute));
//while (!exit)
// Thread.Sleep(1000);
return data;
}
行。发现了一些东西! http://blogs.msdn.com/b/kevinash/archive/2012/02/21/async-ctp-task-based-asynchronous-programming-for-windows-phone.aspx 耶!:)