刪除項這是我的代碼:列表視圖中的自來水
public partial class MyGS: ContentPage {
public MyGS() {
InitializeComponent();
BindingContext = new MyGSViewModel();
}
public class MyGSViewModel: INotifyCollectionChanged {
public event NotifyCollectionChangedEventHandler CollectionChanged;
public ObservableCollection <SchItem> Items {get;private set;}
public MyGSViewModel() {
Items = new ObservableCollection<SchItem>();
//Item Population
public void removeItem(int rid, int lid) {
SchItem myItem = Items[lid];
Items.Remove(myItem);
CollectionChanged ? .Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, myItem));
}
}
public class SchItem {
public int realm_id {get;set;}
public int list_id {get;set;}
public ICommand TapCommand {
get {return new Command(() => {
Debug.WriteLine("COMMAND: " + list_id);
MyGSViewModel gsvm = new MyGSViewModel();
gsvm.removeItem(realm_id, list_id);
});
}
}
}
}
當RemoveItem方法被稱爲視圖不刷新,該項目將不從ListView中刪除,也許問題是關於CollectionChanged但我不知道如何解決這個問題。
注:調試,在Android設備
關於編輯,我得到了'System.In validCastException:指定的轉換無效。 '也許是因爲我在Item定義類中有tap命令? 編輯:我得到了SchItem而不是MyGSViewModel作爲CommandParameter – Segamoto
@Segamoto O權利,因爲'BindingContext'是對象,因爲它在'ListView' ...所以在這種情況下,我一定會推薦使用'Tapped'事件。我會編輯我的答案。 – hvaughan3
@Segamoto現在檢查 – hvaughan3