我从服务器得到回复,Json在哪里有关于街道名称的数据。然后我将响应字符串解析为Json,并将街道名称添加到列表中。我希望此列表显示为自动填充框中的下拉列表,当文本长度等于2时(我按下自动填充框中的第二个字符)。 我也使用Json.Net库。 我使用这段代码:
这是我使用的类(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
}
}
当我在Autocompletebox中输入第二个字符时,它不会显示下拉列表。请帮忙。
答案 0 :(得分: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 */
}