我正在编写一个WP7应用程序,它使用WebClient的DownloadStringAsync方法获取网站信息。DownloadStringCompletedEventHandler会在文本显示之前对其进行解析。
举个例子:
foo() {
...
getAllTheWebsiteInfo()
...
// display the downloaded, parsed text [1]
...
}
getAllTheWebsiteInfo() {
...
DownloadStringAsync()
...
}我遇到的问题是,在点[1]时,文本是空的(默认值)。
有没有办法让foo()知道DownloadStringCompletedEventHandler何时完成了对文本的解析,这样我就可以正确地显示下载的、解析过的文本了?
发布于 2012-03-28 18:27:58
不应在foo方法中设置文本,而应只调用在事件处理程序末尾更新UI的方法,如下所示:
private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
DoParsingStuff(e); // do parsing stuff - may or may not be in it's own method ;)
UpdateUI(); // this would contain your code to update the UI,
// just as the name says
}而不是你的方法,基本上是这样的:
private void foo()
{
// ...
getAllTheWebsiteInfo(); // as shown above
// ??? waiting stuff ???
UpdateUI();
}https://stackoverflow.com/questions/9904706
复制相似问题