Xamarin MVVMCross具有多种类型的CollectionViewCell和数据绑定问题

Xamarin MVVMCross具有多种类型的CollectionViewCell和数据绑定问题,xamarin,xamarin.ios,mvvmcross,Xamarin,Xamarin.ios,Mvvmcross,我是MVVMCross的新手,理论上,在我开始使用UICollectionViewCell之前,它应该不会那么难 我的视图控制器中有多个部分,每个部分都应该绑定到不同类型的数据,我应该怎么做 在ViewModel中,它有一个列表和一个属性,我想使用以下属性填充自定义单元格 List<ClassForCell1> list; private int _valueForCell2; public int ValueForCell2 { get => _valueForCe

我是MVVMCross的新手,理论上,在我开始使用UICollectionViewCell之前,它应该不会那么难

我的视图控制器中有多个部分,每个部分都应该绑定到不同类型的数据,我应该怎么做

在ViewModel中,它有一个列表和一个属性,我想使用以下属性填充自定义单元格

List<ClassForCell1> list;
private int _valueForCell2;

public int ValueForCell2
{
    get => _valueForCell2;
    set => _valueForCell2 = value;
}
这是我的手机

protected CustomCell1(IntPtr handle) : base(handle)
{
    this.DelayBind(() =>
    {
        var set = this.CreateBindingSet<CustomCell1, ClassForCell1>();
        set.Bind(NameLabel).To(m => m.name);
        set.Apply();
    });
}

protected CustomCell2(IntPtr handle) : base(handle)
{
    this.DelayBind(() =>
    {
        // how to bind this one?
        var set = this.CreateBindingSet<CustomCell2, ???>();
        set.Bind(NameLabel).To(a view model's value); // ValueForCell2 in ViewModel
        set.Apply();
    });
}
受保护的CustomCell1(IntPtr句柄):基本(句柄)
{
这个.DelayBind(()=>
{
var set=this.CreateBindingSet();
set.Bind(namelab).To(m=>m.name);
set.Apply();
});
}
受保护的CustomCell2(IntPtr句柄):基本(句柄)
{
这个.DelayBind(()=>
{
//这个怎么绑?
var set=this.CreateBindingSet();
set.Bind(namelab).To(视图模型的值);//视图模型中的ValueForCell2
set.Apply();
});
}
我的问题是:

  • 如何将viewModel的特定属性或列表绑定到特定的单元格或节 下面是我的ViewController中的代码片段,用于绑定ViewController和ViewModel,但它似乎根本不起作用

    var source = new MySource(MyCollectionView, MyViewModel);
    var set = this.CreateBindingSet<MyViewController, MyViewModel>();
    
    var source=newmysource(MyCollectionView,MyViewModel);
    var set=this.CreateBindingSet();
    
  • 对于CustomCell2,如何将标签的文本属性绑定到视图模型的属性?(ValueForCell2)

  • 每个单元格以及父视图模型都需要有单独的视图模型,以显示要显示的项目列表

    由于需要显示两种不同类型的单元格,因此需要一个公共基类来描述它们。为此,您可以创建一个
    BaseCellViewModel
    类,如下所示:

    public abstract class BaseCellViewModel : MvxViewModel
    {
        private string _name;
        public string Name {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }
    }
    
    现在已经有了基类设置,可以为要显示的每个单元创建ViewModels:

    public class FirstCustomCellViewModel : BaseCellViewModel
    {
      //add any properties that are specific to the first type of cell
    }
    
    public class SecondCustomCellViewModel : BaseCellViewModel
    {
      //add any properties that are specific to the second type of cell
    }
    
    现在单元的所有ViewModel都已设置完毕,您可以按如下方式设置父ViewModel:

    public class ListViewModel : MvxViewModel
    {
        private ObservableCollection<BaseCellViewModel> _listItems { get; set; }
        public virtual ObservableCollection<BaseCellViewModel> ListItems {
            get { return _listItems; }
            set { _listItems = value; 
                RaisePropertyChanged(() => ListItems);
            }
        }
    }
    
    protected CustomCell1(IntPtr handle) : base(handle)
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<CustomCell1, FirstCustomCellViewModel>();
            set.Bind(NameLabel).To(vm => vm.Name);
            set.Apply();
        });
    }
    
    protected CustomCell2(IntPtr handle) : base(handle)
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<CustomCell2, SecondCustomCellViewModel>();
            set.Bind(NameLabel).To(vm => vm.Name); 
            set.Apply();
        });
    }
    
    现在在单元格中定义绑定,如下所示:

    public class ListViewModel : MvxViewModel
    {
        private ObservableCollection<BaseCellViewModel> _listItems { get; set; }
        public virtual ObservableCollection<BaseCellViewModel> ListItems {
            get { return _listItems; }
            set { _listItems = value; 
                RaisePropertyChanged(() => ListItems);
            }
        }
    }
    
    protected CustomCell1(IntPtr handle) : base(handle)
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<CustomCell1, FirstCustomCellViewModel>();
            set.Bind(NameLabel).To(vm => vm.Name);
            set.Apply();
        });
    }
    
    protected CustomCell2(IntPtr handle) : base(handle)
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<CustomCell2, SecondCustomCellViewModel>();
            set.Bind(NameLabel).To(vm => vm.Name); 
            set.Apply();
        });
    }
    
    受保护的CustomCell1(IntPtr句柄):基本(句柄)
    {
    这个.DelayBind(()=>
    {
    var set=this.CreateBindingSet();
    set.Bind(namelab).To(vm=>vm.Name);
    set.Apply();
    });
    }
    受保护的CustomCell2(IntPtr句柄):基本(句柄)
    {
    这个.DelayBind(()=>
    {
    var set=this.CreateBindingSet();
    set.Bind(namelab).To(vm=>vm.Name);
    set.Apply();
    });
    }
    
    在具有UICollectionView的ViewController中,您只需绑定源:

    var source = new MyCollectionViewSource(MyCollectionView, MyViewModel);
    var set = this.CreateBindingSet<MyViewController, ListViewModel>();
    set.Bind(source).To(vm => vm.ListItems);
    set.Apply();
    
    var source=新的MyCollectionViewSource(MyCollectionView,MyViewModel);
    var set=this.CreateBindingSet();
    set.Bind(source.To)(vm=>vm.ListItems);
    set.Apply();