Button Command Binding in UWP "XAML with C#"

This sample shows how to bind a command to button in Universal Windows Platform. We will go step by step in this sample.

C# (11.7 MB)
 
 
 
 
 
5 Star
(2)
661 times
Add to favorites
12/21/2015
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • CanExecute not firing
    2 Posts | Last post September 12, 2016
    • Thanks for the great post. I made a small modification to the code:
      
      public class CommandHandler : ICommand
          {
              private Action<object> _action;
              private Predicate<object> _canExecute;
              public CommandHandler(Action<object> action, Predicate<object> canExecute)
              {
                  _action = action;
                  _canExecute = canExecute;
              }
      
              public CommandHandler(Action<object> action)
              {
                  _action = action;
              }
      
              public bool CanExecute(object parameter)
              {
                  return _canExecute == null ? true : _canExecute(parameter);
              }
      
              public event EventHandler CanExecuteChanged;
      
              public void Execute(object parameter)
              {
                  _action(parameter);
              }
          }
      
      And this is how I call it:
      
      public CommandHandler ConnectCommand
              {
                  get
                  {
                      return _connectCommand ?? (_connectCommand = new CommandHandler(param => Connect((string)param), CanConnect));
                  }
              }
      
      The CanExecute is not firing. I understand that this needs to be called explicitly in UWP. I'm not sure how to implement. Do you have any advice?
      
      Thanks!
    • I should note that the above modification is taken from the old RelayCommand.