Question

I get response from server, it is Json where are data about street names. Then I parse response string to Json, and add street names to list. I want that this list show like dropdown in Autocompletebox, when text length equals two(I press second character in Autocompletebox). Also I use Json.Net library. I use this code:

Here is class(JsonWorker) I use:

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;

        }

Here is method(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;
    }

Here is when I call the method above:

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

And when I enter the second character in Autocompletebox, it don't show dropdownlist. Please help.

Était-ce utile?

La solution

Edit

After understanding your use case then what you need is use the Populating event. This event is fired when you want to populate the drop-down with possible matches. To make this called once 2 characters or more have been typed you will also need to set MinimumPrefixLength to 2.

Moreover, change your GetStreets method to take a string param containing the chars in the textbox.

// 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
  // ...
}

What you need is setting MinimumPrefixLength property to 2.

Also move your bindings to the constructor and remove the TextChanged event.

// 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 */
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top