MVVMCross iOS 如何在 TableViewCell 按钮中传递参数
MVVMCross iOS how to pass parameter in TableViewCell button
我的 tableviewcell 中有一个按钮,我试图将 DataContext 作为参数传递给 tableviewcell 中的按钮命令。以便可以在视图模型中执行命令操作。这是我在 tableviewcell class:
中的代码
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 执行清除和添加操作,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 之外,视图并没有失去其绑定,并且表视图显示新更新的行,与 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; }
}
我的 tableviewcell 中有一个按钮,我试图将 DataContext 作为参数传递给 tableviewcell 中的按钮命令。以便可以在视图模型中执行命令操作。这是我在 tableviewcell class:
中的代码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 执行清除和添加操作,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 之外,视图并没有失去其绑定,并且表视图显示新更新的行,与 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; }
}