Command CanExecute bug?

ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

Is it just me, or does binding a command with the optional CanExecute property not work?
Xamarin Forms 3.5.0.129452 - Current on 07feb2019

In this case the button never enables.

                <Button
                    Command="{Binding CopyFilesCommand}"
                    Text="Copy" />
        private ICommand _CopyFilesCommand;
        public ICommand CopyFilesCommand
        {
            get { return _CopyFilesCommand ?? (_CopyFilesCommand = new Command(On_CopyFilesCommand, (x) => CanCopyFiles)); }
        }

        public bool CanCopyFiles
        {
            get
            {
               //Logic to determine true or false
            }
        }

Property being used as the deciding factor is calling both OnPropertyChanged as well as Command.ChangeCanExecute
So a SelectedPath property changes - it yells out that the command should be re-evaluted for 'can' or 'cannot'
                OnPropertyChanged(nameof(CanCopyFiles));
                ((Command)CopyFilesCommand).ChangeCanExecute();

In this case the button never enables.

However if I add one simple line to the XAML and manually bind the IsEnabled to the exact same property - THEN the button enables and disables as expected - proving that all the other code is right.

                <Button
                    Command="{Binding CopyFilesCommand}"
                    IsEnabled="{Binding CanCopyFiles}"
                    Text="Copy" />

Its an easy work around... But the point is that just binding to the command should be enough, provided the command is created knowing what property controls its can factor. Right?

Best Answer

Answers

  • NMackayNMackay GBInsider, University mod

    Yeah,

    You shouldn't have to bind to IsEnabled, need to see if I can replicate that

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @NMackay said:
    Yeah,

    You shouldn't have to bind to IsEnabled, need to see if I can replicate that

    Agreed. Shouldn't have to. And I'm fairly certain I've used it as intended in the past. Which makes me worry/wonder if I should happen to update an app where that is used, will all the UI suddenly break and have everything enabled even when conditions aren't met.

    Would love to hear if you were able to reproduce the issue or not.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JamesLavery
    Thanks for doing that. I really appreciate it. In my head I figured that if it were really broken then there would have been a lot more screaming. I'm just not seeing what's up in that solution.

    Then again, that solution pre-dates Xamarin being part of Microsoft and has undergone a constant upgrade and evolution process and two TFS server upgrades. So who knows what's laying under the hood at this point? {smirk}

  • NMackayNMackay GBInsider, University mod

    @ClintStLaurent said:
    @JamesLavery
    Thanks for doing that. I really appreciate it. In my head I figured that if it were really broken then there would have been a lot more screaming. I'm just not seeing what's up in that solution.

    Then again, that solution pre-dates Xamarin being part of Microsoft and has undergone a constant upgrade and evolution process and two TFS server upgrades. So who knows what's laying under the hood at this point? {smirk}

    Tested Forms 3.5 & 4.0 beta and not seeing that...in Android anyway.

     public class AboutViewModel : BaseViewModel
        {
    
            private bool _cannot = false;
    
            public AboutViewModel()
            {
                Title = "About";
    
                OpenWebCommand = new Command(() => Device.OpenUri(new Uri("https://xamarin.com/platform")),()=>_cannot);
            }
    
            public ICommand OpenWebCommand { get; }
        }
    
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Yes, if it was broken I think there would be wailing and gnashing of teeth - we rely on it!

    Your problem highlights the problem with older apps which get upgraded but not revamped.
  • NMackayNMackay GBInsider, University mod

    @ClintStLaurent said:
    @JamesLavery
    Thanks for doing that. I really appreciate it. In my head I figured that if it were really broken then there would have been a lot more screaming. I'm just not seeing what's up in that solution.

    Then again, that solution pre-dates Xamarin being part of Microsoft and has undergone a constant upgrade and evolution process and two TFS server upgrades. So who knows what's laying under the hood at this point? {smirk}

    Jumping from 2.5.0 to 3.x wasn't straight forward for us..., maybe some refactoring needed.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @NMackay said:
    Jumping from 2.5.0 to 3.x wasn't straight forward for us..., maybe some refactoring needed.

    That wasn't awful for us. And happened some time ago. Our biggest issue one team lead that suffers greatly from "I didn't make it syndrome" - Whether he is assigned a story or not is beside the point, he's going to get in and much about, refactor and rewrite entire subsystems. To be fair a lot of what he wants to do is good in theory. But he's got himself in too many things. So they are all 80%, instead of doing one to completion then moving on. So the DoohickyManager has most of what it needs, but lacks robust error compensation etc.

    In this case I suspect he's trying to do some kind of UI-to-command behavior on a system-wide basis, without telling anyone else he's doing it... and its mucking up everyday behavior.

Sign In or Register to comment.