我正在开发 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
属性时忘记调用 RaiseCanExecuteChanged
。
另外,您不需要设置始终返回 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;
}
}
关于android - 可以在 MvvmCross 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31985256/