Android 可以在MvvmCross中执行
我正在开发一个Xamarin.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
中的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