我目前正在开发一个应用程序,但对 wp7 开发来说相当新,我的主进程调用另一个类,该类使用 StreamReader 来读取网页的内容并分配变量。我遇到的问题是主进程试图在变量被赋值之前使用它们。有什么方法可以让主进程等到 StreamReader 完成
在主线程中,我有:
locationDetails = new LocationResults();
locationDetails.getResults(addressDetails);
然后在 LocationResults 类中
public void getResults(String address)
{
String addy, tmp;
if (address[0] == '+')
{
tmp = address.Substring(1);
addy = baseAddress + tmp + "&sensor=false";
}
else
addy = baseAddress + address + "&sensor=false";
WebClient webClient = new WebClient();
webClient.OpenReadAsync(new Uri(addy));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
String tmp;
var reader = new StreamReader(e.Result);
tmp = reader.ReadToEnd().ToString();
results = tmp;
}