2015-08-13 10 views
3

Я работаю над приложением Xamarin.Android, и я использую MvvmCross. Вот в моем коде DecreaseCommand не работает:CanExecute in 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

Как вы связывании это? Работает ли команда «Увеличение»? –

+0

Да Increasecommand works.when в CanExecuteDecreaseCommand() Я положил {return true}, тогда он работает fine.but, когда я положил {Quantity! = 0}, он не работает –

ответ

1

Вы обновили свой Quantity.

Кроме того, вам не нужно устанавливать 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; 
    } 
} 
+0

Я пробовал, но ошибка всплывает, System.Windows.Input .ICommand не содержит определения для RaisCanExecuteChanged() –

+0

Я изменил ICommand на IMvxCommand и It Works.Thank вы так много. @ Wiliam Barbosa –

 Смежные вопросы

  • Нет связанных вопросов^_^