jonny b
Geek thoughts, dreams, opinions and sharing of lessons learned
  • Blog
  • About
  • Contact

Xamarant: The order of 'IsEnabled' and 'Command' should not matter

3/8/2019

0 Comments

 
Xamarant (noun):  A rant about Xamarin.Forms from the developer trenches.
Picture
Picture
​I would have expected the two buttons above to work exactly the same, and was super surprised when I learned (the hard way) that the order in which you declare the IsEnabled and Command property has major significance when writing your Xamarin.Forms app!

It turns out that if you declare IsEnabled first (as it is in the image above, to the right), the behavior will not at all be what you expect. You absolutely must declare the Command first, and IsEnabled second!
Picture
​​That's freaking nuts.

One might argue that you should not be using IsEnabled at all in an MVVM based app. You should really be using the CanExecute property of the command. This is totally valid, but not everyone will know this, and the IsEnabled is a very simple method of enabling and disabling a button. Both techniques are covered in the Xamarin.Forms documentation, but there are no warnings to indicate that using the IsEnabled property in conjunction with the Command property will radically change the behavior of IsEnabled.

There is hope, however, and lots of it! First of all, there is an active issue on GitHub (as of 2019.03.08, anyway) where others have reported the issue, and that might mean there's a chance the Xamarin team will respond. I hope that at the very least, they will add a little warning note on the documentation of the IsEnabled property and Button class.

Second, I have a sample project up on GitHub for anyone who needs a hand with this. The master branch demonstrates the issue, but there are several other branches demonstrating how to use the CanExecute functionality of an ICommand to enable/disable a button without using the IsEnabled property at all. Hope this will help some devs spend less time troubleshooting this than my team did.
0 Comments



Leave a Reply.

    Author

    Jon Bachelor:  This geek goes all the way to 11.

    Archives

    March 2019
    July 2018
    June 2018
    May 2018
    June 2017
    July 2016
    June 2016
    December 2014
    November 2014
    September 2014
    March 2013
    January 2013
    December 2012
    November 2012
    October 2012

    Categories

    All
    Coding
    Education
    JavaScript
    Mac
    Not Geeky
    Presentations
    Reviews
    Shoutouts
    Tools
    Tutorial
    Xamarin

    RSS Feed

Powered by Create your own unique website with customizable templates.