MVVMCross iOS如何在TableViewCell按钮中传递参数

时间:2018-03-16 22:36:40

标签: ios uitableview mvvmcross observablecollection icommand

我的tableviewcell里面有一个按钮,我试图将DataContext作为参数传递给tableviewcell中的按钮命令。以便可以在视图模型中执行命令操作。这是tableviewcell类中的代码:

public partial class AssociatedEntCell : MvxTableViewCell
{
    public static readonly NSString Key = new NSString(nameof(AssociatedEntCell));

    private const string BindingText = "AssociatedEnt Username";

    public AssociatedEntCell(IntPtr handle) : base(BindingText, handle)
    {
        InitialiseBindings();
    }

    private void InitialiseBindings()
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<AssociatedEntCell, AssociationItemViewModel>();

            set.Bind(btnDeleteAssociation).To(vm => vm.DeleteAssociationCommand).CommandParameter(this.DataContext);
            set.Apply();
        });
    }

    public string AssociatedEnt { get { return lblAssociatedName.Text; } set { lblAssociatedName.Text = value; } }

}

}

第一次完美运作;但是一旦我在关联的ObservableCollection上执行clear和add操作,DataContext就不会更新,我得到了和以前一样的旧DataContext。下面是ObservableCollection的ViewModel片段:

    private MvxObservableCollection<AssociationItemViewModel> associatedEntities;
    public MvxObservableCollection<AssociationItemViewModel> AssociatedEntities
    {
        get { return associatedEntities; }
        set { SetProperty(ref associatedEntities, value); }
    }

    private async Task LoadAssociations()
    {
        Dispatcher.RequestMainThreadAction(() => { 
            AssociatedEntities.Clear();
        });

        //Fetching the list from API
        var res = await businessFacade.GetAssociations(entityID, true);

            if (res == null || !res.Any())
            {
                return;
            }

            foreach (var item in res)
            {
                var associationItem = new AssociationItemViewModel
                {
                    AccountName = item.acname,
                    AccountNum = item.acnum.GetValueOrDefault(),
                    UserID = item.usrid.GetValueOrDefault(),
                    Username = item.usrname,
                    DeleteAssociationCommand = this.DeleteAssociationCommandInit
                };

                Dispatcher.RequestMainThreadAction(() => { 
                AssociatedEntities.Add(associationItem);
                });
            }
        }
    }

除了DataContext之外,视图不会丢失其绑定,并且tableview显示与ObservableCollection中相同的新更新行。

有没有办法获取命令的更新DataContext?或者甚至在对observablecollection进行更改后知道真正的indexPath的任何可能的方法?

1 个答案:

答案 0 :(得分:1)

我最终使用了一个单独的属性来保存索引路径,如下所示:

我的TableviewSource:

   protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        var cell = (AssociatedEntCell)tableView.DequeueReusableCell(AssociatedEntCell.Key);

        if (cell != null)
        {
            cell.Index = indexPath.Row;
        }

        return cell;
    }

我的手机:

public partial class AssociatedEntCell : MvxTableViewCell
{
    public static readonly NSString Key = new NSString(nameof(AssociatedEntCell));

    private const string BindingText = "AssociatedEnt Username; AssociatedUserID UserID";

    public AssociatedEntCell(IntPtr handle) : base(BindingText, handle)
    {
        InitialiseBindings();
    }

    private void InitialiseBindings()
    {
        this.DelayBind(() =>
        {
            var set = this.CreateBindingSet<AssociatedEntCell, AssociationItemViewModel>();

            set.Bind(btnDeleteAssociation).To(vm => vm.DeleteAssociationCommand).CommandParameter(this.Index);

            set.Apply();
        });
    }

    public string AssociatedEnt { get { return lblAssociatedName.Text; } set { lblAssociatedName.Text = value; } }

    public int AssociatedUserID { get; set; }

    public int Index { get; set; }

}