1

コマンドをバインドしてテーブルのセクションでタップを受け取るにはどうすればよいですか? カスタム TableSource で MvxViewController を使用していますが、セクションの UIView を作成するときに VM にバインディングを追加できないようです。

これが私のViewModelです:

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