首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CanExecute in MvvmCross

CanExecute in MvvmCross
EN

Stack Overflow用户
提问于 2015-08-13 10:08:21
回答 1查看 874关注 0票数 4

我正在开发一个Xamarin.Android应用程序,并且正在使用MvvmCross。在我的代码中,DecreaseCommand不起作用:

代码语言:javascript
复制
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没有启动,这段代码有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-13 11:30:33

更新RaiseCanExecuteChanged属性时忘记调用Quantity

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

代码语言:javascript
复制
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;
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31985256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档