1

如何绑定命令以接收对我的表格部分的点击?我正在使用带有自定义 TableSource 的 MvxViewController,但在为部分创建 UIView 时,我似乎无法将绑定添加到我的 VM。

这是我的视图模型:

public class TestViewModel : MvxViewModel
{
    private ObservableCollection<string> _sections;
    public ObservableCollection<string> Sections
    {
        get { return _sections; }
        set { _sections = value; RaisePropertyChanged(() => Sections); }
    }


    private MvxCommand _sectionTappedCommand;
    public ICommand SectionTappedCommand
    {
        get
        {
            _sectionTappedCommand = _sectionTappedCommand ?? new MvxCommand(DoSectionTappedCommand);
            return _sectionTappedCommand;
        }
    }
    private void DoSectionTappedCommand()
    {
        //I want this command somehow to be called when user taps section header
        Debug.WriteLine("Section tapped!");        
    }
}

我的观点:

[Register("TestView")]
public class OrderView : MvxViewController
{
    public override void ViewDidLoad()
    {
        View = new UIView() { BackgroundColor = UIColor.White };
        base.ViewDidLoad();

        var table = new UITableView(new RectangleF(0, 20, 320, 660));
        Add(table);

        var source = new TestTableSource(table);
        table.Source = source;

        var set = this.CreateBindingSet<OrderView, OrderViewModel>();
        // I think here I need to write something like:
        // set.Bind(source).For(s => s.Section.TapAction).To(vm => vm.SectionTappedCommand);**
        set.Bind(source).For(s => s.ItemsSource).To(vm => vm.Sections).OneWay();
        set.Apply();

    }
}

表源:

public class TestTableSource : MvxBaseTableViewSource
{
    // all needed overrides implemented
    private IList<OrderGuest> _sections;
    public IList<OrderGuest> ItemsSource
    {
        get
        {
            return _sections;
        }
        set
        {
            _sections = value;
            ReloadTableData();
        }
    }
    public override UIView GetViewForHeader(UITableView tableView, int section)
    {
        // Do I need to add bindings here?
        var view = new OrderGuestSectionHeader(OrderGuestSectionHeader(tableView, section), () => {
            Debug.WriteLine("selected " + section.ToString());
        });
        return view;
    }

    public override int NumberOfSections(UITableView tableView)
    {
        if (_sections == null)
            return 0;

        return _sections.Count;
    }

    public override string[] SectionIndexTitles(UITableView tableView)
    {
        if (_sections == null)
            return null;

        return _sections.Select(x => x.Name).ToArray();
    }

}

部分标题的子类 UIView:

public sealed class OrderGuestSectionHeader : UIView
{
    private UIButton SectionButton;
    public Action TapAction;

    public OrderGuestSectionHeader(string header, Action tapped)
    {
        Frame = new RectangleF(0, 0, 320, 20);
        BackgroundColor = UIColor.Blue;
        SectionButton = new UIButton(this.Frame);
        SectionButton.TouchUpInside += SectionButton_TouchUpInside;
        SectionButton.Title(header);
        TapAction = tapped;
        Add(SectionButton);
    }

    private void SectionButton_TouchUpInside(object sender, EventArgs e)
    {
        TapAction();
    }
}
4

1 回答 1

0

有几种方法可以实现这种效果。

对于您当前的要求,实现它的最简单方法是仅向 中添加一个命令,TestTableSource并将该命令传递到 Action 处理程序内的节标题。

public class TestTableSource : MvxBaseTableViewSource
{
    public ICommand FooCommand { get; set; }

    // existing code

    public override UIView GetViewForHeader(UITableView tableView, int section)
    {
        var view = new OrderGuestSectionHeader(OrderGuestSectionHeader(tableView, section), () => {
            Debug.WriteLine("selected " + section.ToString());
            if (FooCommand != null) FooCommand.Execute(null);
        });
        return view;
    }
}

然后可以通过添加OrderView绑定将此命令绑定到 ViewModel:

set.Bind(source)
   .For(s => s.FooCommand)
   .To(vm => vm.SectionTappedCommand)
   .OneWay();

如果你想更进一步——如果你想做一个更复杂的绑定——那么你实际上可以DataContext为标题视图设置一个完整的绑定。最简单的方法是继承自MvxView. 我不会在这里详细介绍这一点 - 而是介绍MvxView,请参阅 N+1 视频 - http://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-带有示例源代码的starring.html ,位于https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-32-ViewModels

于 2013-08-30T14:18:43.903 回答