Xamarin表单更新listView itemSource

12

好的,我有一个ListView对象,它有一个List<Filiale>作为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中,我使用SetBinding将标签的Text字段进行了赋值。


您可以从 INotifyPropertyChanged 继承您的 ViewModel(如果正在使用)。 - SoftSan
请查看此链接(http://stackoverflow.com/questions/25364513/can-i-reorder-the-listview-items-on-the-run-in-xamarin-forms),可能会对您有所帮助。 - Femil Shajin
6个回答

20

6

好的,这里是我解决问题的方法。首先,我创建了一个“包装器”,为我作为ItemSource的列表实现了INotifyPropertyChanged,就像这样:

public class Wrapper : INotifyPropertyChanged
    {
        List<Filiale> 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<Filiale> 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 ();
            }

终于我明白了,谢谢! - Mario Galván

2

你可以定义一个基本视图模型,并从INotifyPropertyChanged继承它。

public abstract class BaseViewModel : INotifyPropertyChanged
    {
        protected bool ChangeAndNotify<T>(ref T property, T value, [CallerMemberName] string propertyName = "")
        {
            if (!EqualityComparer<T>.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;
    }

然后在你的视图模型中(例如JM),将继承自BaseViewModel,并且可以创建ObservableCollection<YOURLISTCLASS>列表。

此外,你的视图模型(例如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); }
} 

希望这能帮到您。

2
只需在将System.Collections.Generic.List绑定到ListView之前将其转换为System.Collections.ObjectModel.ObservableCollection即可。

2
将Change List更改为ObservableCollection,并实现INotifyPropertyChanged,以使更改在您的ListView中反映出来。

谁应该实现 INotifyPropertyChanged? - Davide Quaglio
你应该这样做。使用此链接扩展你的项目模型:http://msdn.microsoft.com/zh-cn/library/ms743695(v=vs.110).aspx - Martijn00
你是什么意思? - Martijn00
它被提到过“你应该这样做”,似乎你误读了我的问题。 - Davide Quaglio
我修改整个列表而不是单个对象,有没有一种方法可以做到这一点,而不是让Filiale的每个字段都触发propertychanged? - Davide Quaglio

0

这是我从正在开发的应用程序中提取出来的模式的当前实现,尽可能地简洁。


using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace MyNamespace
{
    // This base view model takes care of implementing INotifyPropertyChanged
    // In your extended View Model classes, use SetValue in your setters.
    // This will take care of notifying your ObservableCollection and hence
    // updating your UI bound to that collection when your view models change.
    public abstract class BaseViewModel : INotifyPropertyChanged
    {
        protected void SetValue(ref T backingField, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer.Default.Equals(backingField, value)) return;
            backingField = value;
            OnPropertyChanged(propertyName);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // Using MvvM, this would likely be a View Model class.
    // However, it could also be a simple POCO model class
    public class MyListItem : BaseViewModel
    {
        private string _itemLabel = "List Item Label";
        public string Label
        {
            get => _itemLabel;
            set => SetValue(ref _itemLabel, value);
        }
    }

    // This is your MvvM View Model
    // This would typically be your BindingContext on your Page that includes your List View
    public class MyViewModel : BaseViewModel
    {
        private ObservableCollection _myListItemCollection
            = new ObservableCollection();

        public ObservableCollection MyListItemCollection
        {
            get { return _myListItemCollection; }
            set => SetValue(ref _myListItemCollection, value);
        }
    }

}




网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接