How to use CanExecute with Mvvmcross

前端 未结 2 540
时光取名叫无心
时光取名叫无心 2020-12-19 01:30

I have a Button

2条回答
  •  攒了一身酷
    2020-12-19 02:15

    To follow up on Stuart's answer, it's easy to support both ICommand.CanExecute along with exposing properties to support Android and iOS Mvx bindings.

    To do this, convert your typical CanExecute() methods to properties, then add handlers to CanExecuteChanged that calls RaisePropertyChanged on the associated property. Then use RaiseCanExecuteChanged as normal and the PropertyChanged event gets fired as well.

        ...
    
        // constructor
        public SomeClass()
        {
    
            DoSomethingCommand = new MvxCommand(OnDoSomething, () => CanDoSomething);
            DoSomethingCommand .CanExecuteChanged += (sender, args) => RaisePropertyChanged(() => CanDoSomething);
        }
    
        public bool CanDoSomething
        {
            get { ... }
        }
    
        ...
    

提交回复
热议问题