Xamarin表单更新listView itemSource

好吧我有一个ListView对象,它有一个List作为ItemSource ,我想在对象列表发生变化时刷新ItemSource 。 ListView有一个个性化的ItemTemplate现在我已经这样做了:

 public NearMe () { list=jM.ReadData (); listView.ItemsSource = list; listView.ItemTemplate = new DataTemplate(typeof(FilialeCell)); searchBar = new SearchBar { Placeholder="Search" }; searchBar.TextChanged += (sender, e) => { TextChanged(searchBar.Text); }; var stack = new StackLayout { Spacing = 0 }; stack.Children.Add (searchBar); stack.Children.Add (listView); Content = stack; } public void TextChanged(String text){ //DOSOMETHING list=newList; } 

正如您在TextChanged方法中看到的,我为前一个分配了一个新列表,但视图中没有任何变化。 在我创建的ViewCell ,我使用ViewCell分配标签的Text字段

您可以将ListView的ItemsSource设置为null,然后再次将其设置为表重新加载。 http://forums.xamarin.com/discussion/18868/tableview-reloaddata-equivalent-for-listview

好的,我是如何解决这个问题的,首先我创建了一个“包装器”,它为我作为ItemSource的列表实现了INotifyPropertyChanged ,如下所示:

 public class Wrapper : INotifyPropertyChanged { List list; JsonManager jM = new JsonManager ();//retrieve the list public event PropertyChangedEventHandler PropertyChanged; public NearMeViewModel () { list = (jM.ReadData ()).OrderBy (x => x.distanza).ToList();//initialize the list } public List List{ //Property that will be used to get and set the item get{ return list; } set{ list = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("List"));// Throw!! } } } public void Reinitialize(){ // mymethod List = (jM.ReadData ()).OrderBy (x => x.distanza).ToList(); } 

然后在NearMe类中:

 Wrapper nearMeVM = new Wrapper(); public NearMe () { Binding myBinding = new Binding("List"); myBinding.Source = nearMeVM; myBinding.Path ="List"; myBinding.Mode = BindingMode.TwoWay; listView.SetBinding (ListView.ItemsSourceProperty, myBinding); listView.ItemTemplate = new DataTemplate(typeof(FilialeCell)); searchBar = new SearchBar { Placeholder="Search" }; searchBar.TextChanged += (sender, e) => { TextChanged(searchBar.Text); }; var stack = new StackLayout { Spacing = 0 }; stack.Children.Add (searchBar); stack.Children.Add (listView); Content = stack; } public void TextChanged(String text){ if (!String.IsNullOrEmpty (text)) { text = text [0].ToString ().ToUpper () + text.Substring (1); var filterSedi = nearMeVM.List.Where (filiale => filiale.nome.Contains (text)); var newList = filterSedi.ToList (); nearMeVM.List = newList.OrderBy (x => x.distanza).ToList (); } else { nearMeVM.Reinitialize (); } 

您可以定义基本视图模型并从INotifyPropertyChangedinheritance它

 public abstract class BaseViewModel : INotifyPropertyChanged { protected bool ChangeAndNotify(ref T property, T value, [CallerMemberName] string propertyName = "") { if (!EqualityComparer.Default.Equals(property, value)) { property = value; NotifyPropertyChanged(propertyName); return true; } return false; } protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; } 

然后在你的viewmodel(例如JM)中将inheritance自BaseViewModel并可以创建ObservableCollection List

此外,ViewModel(例如JM)中的字段应实现如下:

 public const string FirstNamePropertyName = "FirstName"; private string firstName = string.Empty; public string FirstName { get { return firstName; } set { this.ChangeAndNotify(ref this.firstName, value, FirstNamePropertyName); } } 

希望这可以帮助。

将List更改为ObservableCollection并实现INotifyPropertyChanged以使更改反映在ListView中。