android - 可以在 MvvmCross 中执行

标签 android xamarin xamarin.android mvvmcross icommand

我正在开发 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/

相关文章:

android - 为什么我在 logcat 中看不到 Firebase Analytics 日志记录?

android - AppCenter Xamarin.Android 在设备上成功构建和安装但未运行

c# - 在 App.xaml 中使用 ResourceDictionary.MergedDictionaries

android - 在 Xamarin Android 上使用 TabLayout 的 SetOnTabSelectdListerner?

android - 在 android 中配对蓝牙设备的正确顺序是什么?

Android 总是在开始时得到一个新的位置

visual-studio-2010 - Visual Studio 2010 可移植类库不允许我更改目标

android - 将 View 添加到不带 XML 的 View

Android Intent 无法从浏览器启动应用程序

java - 我快失忆了