Я пытаюсь настроить таблицу элементов (UITableView) с кнопкой в каждой ячейке, которая ведет к подробному профилю ее элемента.
Но я не уверен, каков правильный подход кэто в MvvmCross.Вот некоторые из моих идей:
- Представьте выход кнопки
ItemCellView
как общедоступный и свяжите его внутри GetOrCreateCellFor
- Передайте
ShowItemDetailsCommand
в каждый ItemCellView
и связайтеэто там - Используйте простой обратный вызов от
ItemCellView
до ItemsView
вместо привязки - Получите отдельный
MvxViewModel
для каждой ячейки и оттуда вызовите службу навигации
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();
});
}
}