답변

1

몇 가지의 디버깅. 일반적으로 대신 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)); 
    } 
} 

나는 또한 기본 뷰 모델로 OnPropertyChanged 방법과 PropertyChanged 이벤트를 이동 제안 당신의 ViewModels의 모든 있도록 베이스에서 상속받을 수 있으며 모든 코드를 복제 할 필요가 없습니다.

* 편집 : TapCommandSchItem 클래스 내에 정의되어있는 것으로 나타났습니다. 거기에서 명령이 실행될 때마다 새 MyGSViewModel 인스턴스를 새로 작성합니다. 따라서 MyGSViewModel의 모든 항목이 static (권장하지 않음)으로 설정되어 있지 않으면 사용자의 MyGS 페이지에 영향을주지 않습니다. 위에서 제안한 것 외에도 removeItem 메서드에 여러 매개 변수를 전달해야하므로 대신 Tapped 이벤트를 사용하는 것이 좋습니다.

그렇게하려면 ... 당신의 XAML에서

: 당신의 MyGSContentPage에서

<Button Tapped="OnItemTapped"/> 

- 또는

<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 : 지정된 캐스트가 유효하지 않습니다. '아마도 항목 정의 클래스에 tap 명령이 있기 때문에? 편집 : SchISTem이 돌아 왔고 MyGSViewModel이 CommandParameter – Segamoto

+0

@Segamoto O입니다. 왜냐하면'BindingContext'는 'ListView'에 있기 때문에 객체입니다 ... 그래서이 경우에는'Tapped'를 사용하는 것이 좋습니다. 행사. 나는 내 대답을 편집 할 것이다. – hvaughan3

+0

@Segamoto Check now – hvaughan3