我想从webClient_OpenReadCompleted获取结果,我想在getMethod中获得响应。但是在这段代码中,首先getMehod工作,然后,当getMethod完成时,webClient_OpenReadCompleted工作。如何在getMethod中获得结果?
P.S。这一切都在Windows Phone上
public string apiUri = "https://api.vk.com/method/";
public string response = "";
public void getMethod(string parameters)
{
var webClient = new WebClient();
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
string uri = apiUri + parameters + "&access_token=" + access_token;
webClient.OpenReadAsync(new Uri(uri));
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
XDocument xml = XDocument.Load(e.Result);
response = xml.ToString();
}
public void statusGet(string uid)
{
getMethod("status.get.xml?uid" + uid);
}
答案 0 :(得分:0)
如果你想返回数据,我建议你设置 1)自定义EventArgs来保存数据 2)自定义事件。
获取数据后,设置事件参数,然后设置事件,以便订阅者获取数据。
答案 1 :(得分:0)
我没有尝试过,但我问了一位高级c#开发人员,他建议创建bool并写下来:
bool work;
public void getMethod(string parameters)
{
var webClient = new WebClient();
webClient.OpenReadCompleted += webClient_OpenReadCompleted;
string uri = apiUri + parameters + "&access_token=" + access_token;
webClient.OpenReadAsync(new Uri(uri));
work = true;
while(work) { Thread.Sleep(100); }
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
XDocument xml = XDocument.Load(e.Result);
response = xml.ToString();
work = false;
}
现在,问题不是真实的,因为我改变了我的应用程序的架构。