2015-02-05 2 views
0

나는 Mvvm과 MvvmCross에 비교적 익숙하다. iOS에서 문제가 발생했습니다.MvvmCross - 값 변환기가있는 TableViewCell의 바인딩 지연 - DataContext가 올바르게 설정되지 않았습니까?

이 내 내가 AwakeFromNib에 넣어 한 내 MvxTableViewCell - 서브 클래스 바인딩 지연 :

this.DelayBind(() => 
{ 
    var bindingSet = this.CreateBindingSet<MyTableViewCell, MyViewModel>(); 
    bindingSet.Bind(myLabel).To(vm => vm.Name).WithConversion(debugvalueconverter, base.DataContext); 
}); 

내 debugvalueconverter은 다음과 같습니다

public class DebugValueConverter : IMvxValueConverter 
     { 
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
      { 
       System.Diagnostics.Debug.WriteLine("Converting debug: " + value); 
       if (parameter != null) 
       { 
        var vm = (MyViewModel)parameter; 
        System.Diagnostics.Debug.WriteLine("Converting debug - name from vm: " + vm.Name); 
       } 
       return value; 
      } 

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
      { 
       return new NotImplementedException(); 
      } 
     } 

이 몇 가지 이상한 출력을 생성 할 때 I 테이블 뷰에서 드래그 할 때 셀을 재활용하기 시작했습니다.

Converting debug: [Name from the cell that's appearing] 
Converting debug - name from vm: [Name from an old cell which got recycled] 

I. 이 경우 DataContext가 올바르게 설정되지 않은 것처럼 보이지만 name-property가 올바르게 설정되어 있습니다. 왜 이런 일이 일어나는 걸까요?

내 문제는 내 ViewModel (즉, 내 base.DataContext)의 몇 가지 속성에 따라 "고급"계산을 수행 할 변환기를 사용하고 싶다는 것입니다.

MvvmCross의 버그입니까? 아니면 누락 되었습니까? DataContext가 올바르게 설정되지 않은 이유는 무엇입니까?

감사합니다.

답변

1

뷰 모델에서 셀로 보낼 수 있다고는 생각하지 않습니다. 뷰에서 바인딩을 수행하고 뷰에서 뷰 모델의 객체 컬렉션에 셀을 바인딩해야합니다. 일반적으로 나는 같은 것을 할 :

public partial class MainView : MvxViewController 
{ 

    /// <summary> 
    /// Views the did load. 
    /// </summary> 
    /// <summary> 
    /// Called when the View is first loaded 
    /// </summary> 
    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 

     var source = new MvxSimpleTableViewSource(MainTableView, MainInspectionCell.Key, MainInspectionCell.Key); 
     MainTableView.Source = source; 

     var set = this.CreateBindingSet<MainView, MainViewModel>(); 

     set.Bind(source).To(vm => vm.Inspections); 
     set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.ItemSelectedCommand); 

     set.Apply(); 

     MainTableView.ReloadData(); 
} 

을 그리고 나는 보통과 같이 셀의 생성자에서 바인딩을 수행 셀 :이 도움이

public partial class MainInspectionCell : MvxTableViewCell 
{ 
    public static readonly UINib Nib = UINib.FromName("MainInspectionCell", NSBundle.MainBundle); 
    public static readonly NSString Key = new NSString("MainInspectionCell"); 

    public MainInspectionCell(IntPtr handle) : base (handle) 
    { 
     this.DelayBind(() => 
     { 
      var set = this.CreateBindingSet<MainInspectionCell, Inspection>(); 
      set.Bind(InspectionCell).For(v => v.BackgroundColor).To(vm => vm.StatusColor).WithConversion("NativeColor"); 
      set.Apply(); 
     }); 
    } 

    public static MainInspectionCell Create() 
    { 
     return (MainInspectionCell)Nib.Instantiate(null, null)[0]; 
    } 
} 

}

희망을! 주요 문제는 뷰 모델을 거기에없는 셀에 직접 보내려고한다는 것입니다. 이것이 필요한 경우, 액세스해야하는 뷰 모델을 전송하는 콜렉션 오브젝트 주위에 일종의 래퍼를 만들어야합니다.

관련 문제