2012-07-27 15 views
0

獲得OpenReadCompleted結果我想從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
如果你想返回數據

,我建議你設置 1)自定義EventArgs的保存數據 2)自定義事件。

當您獲取數據時,您將設置事件參數,然後設置事件以使訂戶獲取數據。

+0

我沒有嘗試,但我問一個高級C#開發人員,他建議這樣做: – 2012-07-27 12:35:03

0

我沒有嘗試,但我問一個資深的C#開發人員,他建議創建布爾和寫:

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; 
} 

現在的問題是不現實的,因爲我改變了我的應用程序的體系結構。