WP8 LongListSelector - re-assigning ItemsSource has no effect

杀马特。学长 韩版系。学妹 提交于 2019-12-21 09:22:28

问题


I'm using the new Windows Phone 8 LongListSelector control, which has its ItemsControl assigned to a List<Group<object>> as so:

    List<Group<PlacePoint>> searchResults; 

    async void doSearch()
    {
        this.searchResults = await SearchHelper.Instance.getSearchResults(txtSearchTerm.Text);
        longList.ItemsSource = this.searchResults;
    }

Unfortunately, the second time that I search, re-setting the .ItemsSource property has no effect and the control simply displays the old List.

How can I change the binding?


回答1:


It would seem that re-assigning longList.ItemsSource does not have any effect, whether this is a bug or by design I can't say.

However, an easy workaround is simply to use an ObservableCollection> instead and then work with this collection rather than re-assigning the ItemsSource.

Sample code:

    ObservableCollection<Group<PlacePoint>> searchResults = new ObservableCollection<Group<PlacePoint>>();


    public SearchPage()
    {
        InitializeComponent();

        longList.ItemsSource = this.searchResults;
    }

    async void doSearch()
    {
        List<Group<PlacePoint>> tempResults = await SearchHelper.Instance.getSearchResults(txtSearchTerm.Text);

        // Clear existing collection and re-add new results
        this.searchResults.Clear();
        foreach (Group<PlacePoint> grp in tempResults )
        {
            this.searchResults.Add(grp);
        }
    }



回答2:


Sometimes it helps to set the ItemsSource to null and then to your result right after.




回答3:


You need to define your doSearch() method using async for await to function properly.

Try declaring you method like this:

private async Task doSearch() {

}


来源:https://stackoverflow.com/questions/13650788/wp8-longlistselector-re-assigning-itemssource-has-no-effect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!