1

にリストビュー内の項目を削除します。RemoveItemメソッドが呼び出されるとビューは更新されません、アイテムが何のリストビューから削除されますこれは私のコードであるタップ

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); 
     }); 
    } 
    } 
} 
} 

、多分問題がて、CollectionChangedについてですが、私はそれを修正する方法を取得しません。

注:Androidデバイスでのデバッグ

答えて

1

物事のカップル。 INotifyCollectionChangedの代わりに通常System.ComponentModel.INotifyPropertyChangedが使用されます。あなたはそれに切り替えても、より一般的な結合パターンを実装する場合は、あなたのViewModelは次のようになります。

public class MyGSViewModel: INotifyPropertyChanged { 
    public event PropertyChangedEventHandler PropertyChanged; 

    public ObservableCollection <SchItem> Items { 
     get { return _items; } 
     private set { 
      if(_items != value) { 
       _items = value; 
       OnPropertyChanged(); //Execute the event anytime an object is removed or added 
      } 
     } 
    } 

    public MyGSViewModel() { 
     Items = new ObservableCollection<SchItem>(); 
     //Item Population 
    } 

    public void removeItem(int rid, int lid) { 
     SchItem myItem = Items[lid]; 
     Items.Remove(myItem); //If an object is removed, the OnPropertyChanged() method will be run from 'Items's setter 
    } 

    protected virtual void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) { 
     PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); 
    } 
} 

私はまた、あなたのviewmodelsのすべてのことができるように、基本のViewModelにOnPropertyChanged方法とPropertyChangedイベントを移動することをお勧めベースから継承することができ、どこにでもそのコードを複製する必要はありません。

*編集:あなたのがあなたのSchItemクラス内に定義されていることに気づいただけです。そこには、そのコマンドが実行されるたびに新しいMyGSViewModelインスタンスが新しくなります。したがって、MyGSViewModelのすべてが静的に設定されていない限り(これはお勧めしません)、これはあなたのMyGSページには影響しません。私が上記で提案したものに加えて、removeItemメソッドに複数のパラメータを渡す必要があるので、の代わりにTappedイベントを使用することをお勧めします。

はそれを行うには...あなたのXAMLで

:あなたのMyGSContentPage

<Button Tapped="OnItemTapped"/> 

-OR-

<Label> 
    <Label.GestureRecognizers> 
    <TapGestureRecognizer Tapped="OnItemTapped"/> 
    </Label.GestureRecognizers> 
</Label> 

:編集について

public partial class MyGS : ContentPage { 

    private MyGSViewModel _viewModel; 

    public MyGS() { 
     InitializeComponent(); 
     BindingContext = _viewModel = new MyGSViewModel(); 
    } 

    private void OnItemTapped(object sender, EventArgs e) { 
     SchItem item = (SchItem)((Image)sender).BindingContext; 

     if(item == null) { return; } 

     _viewModel.removeItem(item.realm_id, item.list_id); 
    } 
} 
+0

、私は 'System.In validCastException:指定されたキャストが無効です。 '多分私はアイテム定義クラスでタップコマンドを持っているので? 編集:私はSchISTemを戻しましたが、MyGSViewModelはCommandParameterとしてではありません。 – Segamoto

+0

@Segamoto O right、 'BindingContext'はオブジェクトです。リストビューなのでオブジェクトです...その場合、私は間違いなく' Tapped'イベント。私は私の答えを編集します。 – hvaughan3

+0

@Segamoto Check now – hvaughan3

関連する問題