Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 可以在MvvmCross中执行_Android_Xamarin_Xamarin.android_Mvvmcross_Icommand - Fatal编程技术网

Android 可以在MvvmCross中执行

Android 可以在MvvmCross中执行,android,xamarin,xamarin.android,mvvmcross,icommand,Android,Xamarin,Xamarin.android,Mvvmcross,Icommand,我正在开发一个Xamarin.Android应用程序,我正在使用MvvmCross。此处我的代码中的DecreaseCommand不起作用: public class CartItemViewModel : MvxNotifyPropertyChanged { private int quantity = 0; public CartItemViewModel() { IncreaseCommand = new MvxComman

我正在开发一个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
没有启动,这段代码可能有什么问题?

当您更新
Quantity
属性时,您忘记调用
RaiseCanecuteChanged

另外,您不需要设置总是返回true的
CanExecute

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

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

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

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


    private void ExecuteIncreaseCommand()
    {
         Quantity++;
    }

    private void ExecuteDecreaseCommand()
    {
        Quantity--;
    }

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

你是怎么装订的?Increase命令有效吗?是Increasecommand有效。当我在CanExecutedCreasCommand()中放入{return true}时,它工作正常。但当我放入{Quantity!=0}时,它不工作,但会弹出错误,System.Windows.Input.ICommand不包含RaisCanExecuteChanged()的定义我已将ICommand更改为IMvxCommand,它可以正常工作。非常感谢。@wiliam Barbosa