2013-05-30 28 views
0

我從服務器得到響應,它是Json,其中有關於街道名稱的數據。然後我將響應字符串解析爲Json,並將街道名稱添加到列表中。我希望當文本長度等於2時(我按下Autocompletebox中的第二個字符),此列表將顯示在自動完成框中的下拉列表中。 另外我使用Json.Net庫。 我使用此代碼:不要在Windows Phone中的自動完成框中顯示下拉列表

下面是類(JsonWorker)使用:

class JsonWorker 
    { 

     public async Task<HttpWebResponse> send(string requestUrl, JObject jsonObjesct) 
     { 
      HttpWebRequest request = (HttpWebRequest) WebRequest.Create(requestUrl); 
      request.ContentType = "text/plain; charset=utf-8"; 
      request.Method = "POST"; 

      byte[] jsonAsBytes = Encoding.UTF8.GetBytes(jsonObjesct.ToString()); 

      Stream x = await request.GetRequestStreamAsync(); 
      await x.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length); 
      x.Close(); 

      HttpWebResponse response = (HttpWebResponse) (await request.GetResponseAsync()); 
      return response; 
     } 

     public async Task<string> get(
      HttpWebResponse response) 
     { 
      var stream = response.GetResponseStream(); 
      var sr = new StreamReader(stream); 
      string str_responsefromjson = await sr.ReadToEndAsync(); 
      sr.Close(); 
      stream.Close(); 

      return str_responsefromjson; 

     } 

這裏是方法(GetSteets):

private async Task<List<string>> GetStreets() 
    { 
     JObject jo = new JObject(); 
     jo.Add("chars", AutoCompleteBox_Streets.Text); 
     jo.Add("city_id", "1"); 

     JsonWorker jWorker = new JsonWorker(); 
     var response = await jWorker.send("website", jo); 
     string str_responseformjson = await jWorker.get(response); 

     jo = JObject.Parse(str_responseformjson); 


     JArray ja = (JArray)jo["street"]; 


     List<string> list_Streets = new List<string>(); 


     foreach (var elem in ja) 
     { 

      list_Streets.Add(elem["title"].ToString()); 
     } 


     return list_Streets; 
    } 

下面是當我打電話上述方法:

private async void AutoCompleteBox_Streets_TextChanged(object sender, RoutedEventArgs e) 
     { 
      if (AutoCompleteBox_Streets.Text.Length.Equals(2)) 
      { 
       AutoCompleteBox_Streets.ItemsSource = await GetStreets(); 
       //On the string of code above in debug, ItemSource contains list of streets 
      } 
     } 

而當我在自動完成框中輸入第二個字符時,它不顯示下拉列表。請幫忙。

+0

此外,如果我將FilterMode更改爲包含,它會在輸入3或4個字符時啓動show drop down。一些神奇的... – vasa911

回答

1

編輯

瞭解你的使用情況後,那麼你需要的是使用Populating事件。當您想用可能的匹配填充下拉菜單時,會觸發此事件。爲了讓這個被調用一次輸入2個字符或更多,您還需要將MinimumPrefixLength設置爲2.

此外,更改您的GetStreets方法,以包含文本框中的字符的參數string

// Your page Loaded event. Bind this event in your xaml. 
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) { 
    AutoCompleteBox_Streets.MinimumPrefixLength = 2; 
    AutoCompleteBox_Streets.Populating += AutoComplete_Populating; 
} 

private async void AutoComplete_Populating(object sender, PopulatingEventArgs e) { 
    // e.Parameter will contain the chars in your textbox. 
    AutoCompleteBox_Streets.ItemsSource = 
     await GetStreets(HttpUtility.UrlEncode(e.Parameter)); 
    AutoCompleteBox_Streets.PopulateComplete(); 
} 

private async Task<List<string>> GetStreets(string chars) { 
    JObject jo = new JObject(); 
    jo.Add("chars", chars); 
    // Rest of your method code 
    // ... 
} 

你需要的是MinimumPrefixLength屬性設置爲2

而且移動你的綁定構造函數和刪除TextChanged事件。

// Your constructor 
public MyPage() { 
    InitializeComponent(); 
    BindStreetNames(); 
} 

private async void BindStreetNames() { 
    AutoCompleteBox_Streets.ItemsSource = await GetStreets(); 
    AutoCompleteBox_Streets.MinimumPrefixLength = 2; 
} 

private async void AutoCompleteBox_Streets_TextChanged(object sender, RoutedEventArgs e) { 
    /* Remove this handler */ 
} 
+0

我需要從服務器** **只有**後輸入​​兩個字符的街道列表。我試圖使用MinimumPrefixLength,但它沒有幫助。 – vasa911

+0

不建議在您的TextChanged事件中進行Web請求調用。您需要考慮到遠程通話可能需要不同時間,具體取決於網絡狀況,因此可以滿足您的要求。上面的代碼將在輸入2個字符後彈出列表。 –

+0

我需要在我的請求中發送兩個字符。只有當我發送兩個字符時,我才能得到迴應。所以我在輸入兩個字符後打電話給GetSteets。 – vasa911

相關問題