title: Button command binding doesn't work in Xamarin.Forms
body: <p>I'd like to bind a command to the command property of my button. This seemed pretty straightforward since I've done this many times before in WPF and the method here is very similar. Let me show some code snippets.</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
           x:Class="MyApp.View.CustomPage"
           Title="Something"&gt;
  
  &lt;ContentPage.Content&gt;
      &lt;StackLayout&gt;
          &lt;Button x:Name="numBtn" Text="Increase number" Command="{Binding IncreaseCommand}" /&gt;
          &lt;Label x:Name="numLabel" Text="{Binding numberText}" /&gt;
      &lt;/StackLayout&gt;
  &lt;/ContentPage.Content&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p><strong>Code-behind</strong></p>
  
  <pre><code>public partial class CustomPage : ContentPage
  {   
      public CustomPage ()
      {
          InitializeComponent ();
          BindingContext = ViewModelLocator.ViewModel();  //ViewModelLocator is singleton, gives
                                                          //you a ViewModel instance
      }
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>public ICommand IncreaseCommand { get; private set; }
  private int number;
  public string numberText { get; private set;}
  </code></pre>
  
  <p>the constructor:</p>
  
  <pre><code>public ViewModel()
  {
      IncreaseCommand = new Command (() =&gt; IncreaseExecuted ());
      number = 0;
      numberText = number.ToString ();
      OnPropertyChanged (numberText);
  }
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>private void IncreaseExecuted()
  {
      number++;
      numberText = number.ToString ();
      OnPropertyChanged (numberText);
  }
  </code></pre>
  
  <p>When I run the app using the Xamarin Android Player (KitKat) I see the button, and the label reading 0. Then I press the button and nothing happens. I tried checking what happens with breakpoints but the app doesn't pause, not even when they're in the constructor of my ViewModel. I guess it's something to do with the emulator. Anyway, I think the binding is ok since I can see a "0" on the screen. What could be the problem? Let me show my ViewModelBase class just in case:</p>
  
  <p><strong>ViewModelBase</strong></p>
  
  <pre><code>public abstract class ViewModelBase : INotifyPropertyChanged
  {
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected virtual void OnPropertyChanged(String propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>Maybe my <code>numberText</code> property doesn't get updated when I call <code>OnPropertyChanged</code>? But I've used the exact same ViewModelBase class several times before and it always worked fine. One last thing, my <code>CustomPage</code> page is wrapped inside a <code>NavigationPage</code> which is a child of a <code>TabbedPage</code>:</p>
  
  <p><strong>MainPage.xaml.cs</strong></p>
  
  <pre><code>this.Children.Add (new NavigationPage (new CustomPage ()) {Title="Something"} );
  </code></pre>
  
  <p>This shouldn't affect anything but there it is just in case. So what's wrong with my command binding? Thank you in advance!</p>