AutoCompleteBox从HttpWebRequest加载数据

时间:2013-02-21 15:46:31

标签: c# windows-phone-7 silverlight-toolkit autocompletebox

我希望AutoCompleteBox通过服务器上的请求完成地址。 我有这个方法来填充我的AutoCompleteBox:

private async void getNearStreets()
        {
            if (acbAddress.Text.Length > 2)
            {
                ApiRequest request = new ApiRequest("hintAddress", new HintAddress(appSettings.InstanceId, acbAddress.Text, appSettings.SmsCode));
                var postData = JsonConvert.SerializeObject(request);
                var response = await HttpHelper.SendRequestGetResponse(postData);
                ApiResponseTest apiResponse = (ApiResponseTest)JsonConvert.DeserializeObject<ApiResponseTest>(response);
                var wordList = this.Resources["autoCompleteWordList"] as AutoCompleteWordList;
                wordList.Clear();
                foreach (var adresa in apiResponse.data.result)
                {
                    HintAddressResponse adrResponse = (HintAddressResponse)JsonConvert.DeserializeObject<HintAddressResponse>(adresa.ToString());
                    wordList.Add(adrResponse.street);
                }
            }
        }

这是我的班级:

public class AutoCompleteWordList : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection<string> _listOfAddresses;
    public ObservableCollection<string> ListOfAddresses
    {
        get { return _listOfAddresses; }
        set
        {
            _listOfAddresses = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("ListOfAddresses"));
        }
    }

    public AutoCompleteWordList()
    {
        ListOfAddresses = new ObservableCollection<string>();
    }

    public void Add(string address)
    {
        ListOfAddresses.Add(address);

    }

    public void Clear()
    {
        ListOfAddresses.Clear();
    }
}

并查看:

<phone:PhoneApplicationPage.Resources>
    <data:AutoCompleteWordList x:Key="autoCompleteWordList" />
</phone:PhoneApplicationPage.Resources>

<toolkit:AutoCompleteBox x:Name="acbAddress" VerticalAlignment="Top"
                             ItemsSource="{Binding Source={StaticResource autoCompleteWordList}, Path=ListOfAddresses}" 
                             TextChanged="acbAddress_TextChanged"/>

我的问题是我正在下载数据,我将它们添加到集合中但DropDownDialog没有显示。我想我必须提醒我有新数据,但我不知道如何。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

当我第一次看msdn时,我错过了PopulateComplete方法。但这就是我需要的东西,当我在方法结束时添加它时,它可以工作。