MVVMCross iOS:如何使用 CreateBindingSet 在自定义 TableViewSource 上触发 RowSelected? [英] MVVMCross iOS: How to trigger RowSelected on a Custom TableViewSource using CreateBindingSet?

查看:14
本文介绍了MVVMCross iOS:如何使用 CreateBindingSet 在自定义 TableViewSource 上触发 RowSelected?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是针对 iOS 中的 MVVMCross.我有一个自定义的 TableView 和一个 detailView.如何绑定我的showDetailCommand",以便当用户单击 TableView 上的行之一并触发 RowSelected 以切换到 detailViewModel 时?

My question is for MVVMCross in iOS. I have a custom TableView and a detailView. How can I bind my "showDetailCommand", so when user click on one of the rows on TableView with trigger RowSelected to switch to detailViewModel?

这是我的代码结构:

public class SearchResultsViewModel: MvxViewModel
{
    private MvxCommand _showDetailCommand;
    public IMvxCommand ShowDetailCommand
    {
        get
        {
            _showDetailCommand = _showDetailCommand ?? new MvxCommand(ShowDetailCommandHandler);
            return _showMapCommand;
        }
    }

    private void ShowDetailCommandHandler()
    {
        ShowViewModel<ResultDetailViewModel>(new
            {
                city = _filter.City,
                state = _filter.State,
                interstate = _filter.Interstate,
                travelPlaza = _filter.SearchTravelPlaza,
                hasCatScale = _filter.HasCatScale,
                hasWashoutExpress = _filter.HasWashoutExpress,
                hasUndercarriage = _filter.HasUndercarriage,
                nearest = _nearest
            });
    }
}

[Register("SearchResults")]
public class SearchResultsView : MvxTableViewController
{
    public override void ViewDidLoad()
    {
                Title = "List";
                base.ViewDidLoad();
                var source = new TableViewSource(TableView);

                var bindings = this.CreateBindingSet<SearchResultsView, SearchResultsViewModel>();
                bindings.Bind(source).To(vm => vm.Items);
                bindings.Apply();

                TableView.BackgroundColor = UIColor.Clear;
                TableView.ShowsVerticalScrollIndicator = false;
                TableView.ScrollEnabled = true;
                TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
                TableView.Source = source;

                TableView.ReloadData();

    }

    public class TableViewSource : MvxTableViewSource
    {
        public TableViewSource(UITableView tableView)
            : base(tableView)
        {
            tableView.RegisterClassForCellReuse(typeof(TableViewCell), TableViewCell.CellIdentifier);
        }

        public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            var item = GetItemAt(indexPath);
            return TableViewCell.CellHeight(item);
        }

        protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
        {
            var cell = tableView.DequeueReusableCell(TableViewCell.CellIdentifier) ?? new TableViewCell();
            return cell;
        }
    }

}

[Register("TableViewCell")]
public class TableViewCell : MvxTableViewCell
{

    public static readonly NSString CellIdentifier = new NSString("TableViewCell");

     public TableViewCell()
        : base()
    {
        Initialise();
    }

    public TableViewCell(IntPtr handle)
        : base(handle)
    {
        Initialise();
    }

    private void Initialise()
    {

        var titleLabel = new UILabel(new RectangleF(10, 2, 200, 25));
        Add(titleLabel);

        this.DelayBind(() =>
        {
            var bindings = this.CreateBindingSet<TableViewCell, SearchResultsItemViewModel>();
            bindings.Bind(titleLabel).For(x => x.Text).To(vm => vm.Name);
            bindings.Bind(??).For(x => x.SelectionChangeCommand).To(vm => vm.showDetailCommand); // what should be in this line ??
            bindings.Apply();
        });

    }

}

推荐答案

您可以将视图模型命令绑定到表源上的 SelectionChangedCommand.

You can bind a view model command to the SelectionChangedCommand on the table source.

此技术在几个示例中得到了演示 - 例如

This technique is demonstrated in several samples - e.g

// command
new Cirrious.MvvmCross.ViewModels.MvxCommand<DilbertItem>((dilbertItem) => 
{
   // note that we can only pass an id here - do *not* serialiase the whole dilbertItem
   ShowViewModel<DilbertDetailViewModel>(new { id = dilbertItem.Id });
});

// binding
set.Bind(source)
   .For(s => s.SelectionChangedCommand)
   .To(s => s.ShowDetailCommand);

或者,您也可以将命令放在列表项中,然后可以在每个单元格内绑定到 SelectedCommand(或单元格内的其他一些自定义事件).

Alternatively, you can also put commands inside your list items and can then bind within each cell to a SelectedCommand (or some other custom event within your cell).

有关此示例,请查看一些具有菜单的示例 - 例如ValueConversion 示例

For examples of this, look at some of the samples which have menus - e.g. the ValueConversion sample

这篇关于MVVMCross iOS:如何使用 CreateBindingSet 在自定义 TableViewSource 上触发 RowSelected?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆