【发布时间】:2016-11-21 18:18:37
【问题描述】:
这是我的代码,当我按下与 Clicked 属性 addische 绑定的按钮时,新项目被添加到 ListView 但我在列表中看不到它。
public partial class GS : ContentPage
{
private GSViewModel _viewModel;
public GS()
{
InitializeComponent();
BindingContext = _viewModel = new GSViewModel();
}
private void addische(object sender, EventArgs e)
{
_viewModel.newItemAdded();
}
public class GSViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<schItem> _scheListItem;
public ObservableCollection<schItem> Items { get { return _scheListItem; }
private set
{
if (_scheListItem != value)
{
_scheListItem = value;
OnPropertyChanged();
}
}
}
public void newItemAdded()
{
Items.Add(new schItem
{
realm_id = 133,
list_id = 33
});
}
public GSViewModel()
{
Items = new ObservableCollection<schItem>();
initListView();
}
public void initListView()
{
//get data
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class schItem
{
public int realm_id { get; set; }
public int list_id { get; set; }
}
}
也许我错过了 INotifyPropertyChanged 类的东西。
我正在 Android 设备中调试
【问题讨论】:
-
需要您的 xaml 才能查看绑定
标签: android listview xamarin observablecollection inotifypropertychanged