1

这是我的代码:

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 设备中调试

4

1 回答 1

1

有几件事。通常System.ComponentModel.INotifyPropertyChanged用来代替INotifyCollectionChanged. 如果您切换到该模式并实现更常见的绑定模式,您的 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));
    }
}

我还建议将OnPropertyChanged方法和PropertyChanged事件移动到基​​础 ViewModel 中,以便您的所有 ViewModel 都可以从基础继承,并且您不会在任何地方重复该代码。

*编辑:刚刚注意到你在你的班级中有你的TapCommand定义。每次运行该命令SchItem时,您都会在其中新建一个新实例。MyGSViewModel因此,除非您的所有内容都MyGSViewModel设置为静态(我不建议这样做),否则这不会影响您的MyGS页面。除了我上面的建议之外,我建议使用Tapped事件而不是 a,Command因为您需要将多个参数传递到您的removeItem方法中。

要做到这一点...

在您的 XAML 中:

<Button Tapped="OnItemTapped"/>

-或者-

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

在你的MyGS ContentPage

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);
    }
}
于 2016-11-20T17:10:55.077 回答