2015-08-13 33 views
3

我正在使用Xamarin.Android應用程序,並且正在使用MvvmCross。在這裏,在我的代碼DecreaseCommand不工作:可以在MvvmCross中執行

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不點火,這可能是錯誤的代碼?

+0

你是如何結合的? Doe增加命令工作嗎? –

+0

是增加命令work.when在CanExecuteDecreaseCommand()我把{返回true}然後它工作正常。但是當我把{Quantity!= 0}它不起作用 –

回答

1

當您更新Quantity屬性時,您忘記了撥打RaiseCanExecuteChanged

另外,你不需要設置CanExecute總是返回true:

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; 
    } 
} 
+0

我試過但彈出錯誤System.Windows.Input .ICommand不包含RaisCanExecuteChanged()的定義 –

+0

我已將ICommand更改爲IMvxCommand及其工作。非常感謝您。@ wiliam Barbosa –

相關問題