可以在MvvmCross

时间:2015-08-13 10:08:21

标签: android xamarin xamarin.android mvvmcross icommand

我正在使用Xamarin.Android应用程序,我使用的是MvvmCross。我的代码中DecreaseCommand无效:

public class CartItemViewModel : MvxNotifyPropertyChanged
{      
    private int quantity = 0;      

    public CartItemViewModel()
    {
        IncreaseCommand = new MvxCommand(ExecuteIncreaseCommand, CanExecuteIncreaseCommand);
        DecreaseCommand = new MvxCommand(ExecuteDecreaseCommand, CanExecuteDecreaseCommand);
        Delete = new MvxCommand (() => {Quantity++;});
    }        

    public int Quantity
    {
        get { return quantity; }
        set
        {
            quantity = value;
            RaisePropertyChanged("Quantity");
            RaisePropertyChanged("SubTotal");
        }
    }

    public ICommand IncreaseCommand { get; set; }
    public ICommand DecreaseCommand { get; set; }
    public ICommand Delete { get; set; }


    private void ExecuteIncreaseCommand()
    {
         Quantity++;
    }

    private bool CanExecuteIncreaseCommand()
    {
        return true;
    }

    private void ExecuteDecreaseCommand()
    {
        Quantity--;
    }

    private bool CanExecuteDecreaseCommand()
    {
        return Quantity > 0;
    }


}

我怀疑CanExecuteDecreaseCommand没有解雇,这段代码可能出错?

1 个答案:

答案 0 :(得分:1)

您在更新validateJson媒体资源时忘记致电RaiseCanExecuteChanged

另外,您无需设置始终返回true的Quantity

CanExecute