ICommand.Execute方法(对象)

时间:2017-07-13 14:48:17

标签: c# android navigation mvvmcross execute

如果我用我的视图中的参数点击执行

GoToNextScreen.Execute(selectedSubMenuIndex);

这会调用我的ViewModel中的方法

        private MvxCommand _goSecondCommand;
        public IMvxCommand GoSecondCommand
        {
            get
            {
                _goSecondCommand = _goSecondCommand ?? new MvxCommand(DoGoSecond);
                return _goSecondCommand;

                //try to call a command that navigates
            }
        }

        public void DoGoSecond()
        {
            ShowViewModel<OnlineGroceryShoppingViewModel>(); //action to go to the second view
        }

但是当我单步执行时它会直接跳转到DoGoSecond,如何在DoGoSecond方法中访问传递的参数selectedSubMenuInde​​x?

PS, 我有

var set = this.CreateBindingSet<HomeView, HomeViewModel>();
set.Bind(this).For(v => v.GoToNextScreen).To(vm => vm.GoSecondCommand);
set.Apply();

所以GoToNextScreen调用GoSecondCommand

1 个答案:

答案 0 :(得分:2)

您需要使用MvxCommand的通用版本:

private MvxCommand<int> _goToSecondCommand;
public ICommand GoToSecondCommand =>
    _goToSecondCommand = _goToSecondCommand ?? new MvxCommand<int>(DoGoToSecond);

private void DoGoToSecond(int index)
{
    // do stuff with index
}

然后,您可以将该索引传递到OnlineGroceryShoppingViewModel

ShowViewModel<OnlineGroceryShoppingViewModel>(new { index = index });

然后在OnlineGroceryShoppingViewModel方法的Init中获取它:

public void Init(int index)
{
    // do stuff based on index
}