MVVMCross iOS如何在TableViewCell按钮中传递参数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MVVMCross iOS如何在TableViewCell按钮中传递参数相关的知识,希望对你有一定的参考价值。
我的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的任何可能的方法?
答案
我最终使用一个单独的属性来保存索引路径,如下所示:
我的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; }
}
以上是关于MVVMCross iOS如何在TableViewCell按钮中传递参数的主要内容,如果未能解决你的问题,请参考以下文章
MvvmCross 4 和 Xamarin.iOS -> 使用 Storyboard 时如何从 Core 加载视图控制器?