I'm trying to set up a table of items (UITableView) with a button in every cell that navigates to a detailed profile of its item.
But I'm not sure what's the correct approach to this in MvvmCross. Some of my ideas:
- Expose the
ItemCellView
's button outlet as public and bind it insideGetOrCreateCellFor
- Pass the
ShowItemDetailsCommand
into eachItemCellView
and bind it there - Use a simple callback from
ItemCellView
to theItemsView
instead of a binding - Get a separate
MvxViewModel
for each cell and call the navigation service from there
public class Item
{
public string Name { get; set; }
}
public class ItemsViewModel : MvxViewModel
{
public List<Item> Items { get; }
public MvxCommand ShowItemDetailsCommand { get; }
readonly IMvxNavigationService _navigationService;
readonly IDatabaseService _databaseService;
public ItemsViewModel(IMvxNavigationService navigationService, IDatabaseService databaseService)
{
ShowItemDetailsCommand = new MvxCommand(ShowItemDetails);
_navigationService = navigationService;
_databaseService = databaseService;
Items = _databaseService.SelectItems();
}
void ShowItemDetails()
{
// not sure how "item" gets here so far
_navigationService.Navigate<ItemDetailsViewModel, Item>(item);
}
}
public partial class ItemsView : MvxTableViewController<ItemsViewModel>
{
public ItemsView() : base("ItemsView", null) {}
public override void ViewDidLoad()
{
base.ViewDidLoad();
TableView = View as UITableView;
var source = new TableViewSource(TableView);
var bindings = this.CreateBindingSet<ItemsView, ItemsViewModel>();
bindings.Bind(source).To(vm => vm.Items);
bindings.Apply();
TableView.Source = source;
TableView.ReloadData();
}
public class TableViewSource : MvxTableViewSource
{
public TableViewSource(UITableView tableView) : base(tableView)
{
TableView.RegisterNibForCellReuse(UINib.FromName("ItemCellView", NSBundle.MainBundle), ItemCellView.kCellId);
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
return TableView.DequeueReusableCell(ItemCellView.kCellId, indexPath) as ItemCellView;
}
}
}
public partial class ItemCellView : MvxTableViewCell
{
public const string kCellId = "item_cell";
// also has an [Outlet] UIButton in the .designer.cs part
public ItemCellView(IntPtr handle) : base(handle)
{
this.DelayBind(() =>
{
var bindings = this.CreateBindingSet<ItemCellView, Item>();
bindings.Bind(Name).To(i => i.Name);
bindings.Apply();
});
}
}