Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7.  
  8. namespace TouristApp_V3.Common
  9. {
  10.     public class RelayCommand : ICommand
  11.     {
  12.         private Action<object> _action;
  13.  
  14.         public RelayCommand(Action<object> action)
  15.         {
  16.             _action = action;
  17.         }
  18.  
  19.         #region ICommand Members
  20.         #endregion
  21.  
  22.    
  23.     /// <summary>
  24.     /// A command whose sole purpose is to relay its functionality
  25.     /// to other objects by invoking delegates.
  26.     /// The default return value for the CanExecute method is 'true'.
  27.     /// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
  28.     /// <see cref="CanExecute"/> is expected to return a different value.
  29.     /// </summary>
  30.    
  31.         private readonly Action _execute;
  32.         private readonly Func<bool> _canExecute;
  33.  
  34.         /// <summary>
  35.         /// Raised when RaiseCanExecuteChanged is called.
  36.         /// </summary>
  37.         public event EventHandler CanExecuteChanged;
  38.  
  39.         /// <summary>
  40.         /// Creates a new command that can always execute.
  41.         /// </summary>
  42.         /// <param name="execute">The execution logic.</param>
  43.         public RelayCommand(Action execute)
  44.             : this(execute, null)
  45.         {
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Creates a new command.
  50.         /// </summary>
  51.         /// <param name="execute">The execution logic.</param>
  52.         /// <param name="canExecute">The execution status logic.</param>
  53.         public RelayCommand(Action execute, Func<bool> canExecute)
  54.         {
  55.             if (execute == null)
  56.                 throw new ArgumentNullException("execute");
  57.             _execute = execute;
  58.             _canExecute = canExecute;
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
  63.         /// </summary>
  64.         /// <param name="parameter">
  65.         /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
  66.         /// </param>
  67.         /// <returns>true if this command can be executed; otherwise, false.</returns>
  68.         public bool CanExecute(object parameter)
  69.         {
  70.             return _canExecute == null ? true : _canExecute();
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Executes the <see cref="RelayCommand"/> on the current command target.
  75.         /// </summary>
  76.         /// <param name="parameter">
  77.         /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
  78.         /// </param>
  79.         public void Execute(object parameter)
  80.         {
  81.             _execute();
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Method used to raise the <see cref="CanExecuteChanged"/> event
  86.         /// to indicate that the return value of the <see cref="CanExecute"/>
  87.         /// method has changed.
  88.         /// </summary>
  89.         public void RaiseCanExecuteChanged()
  90.         {
  91.             var handler = CanExecuteChanged;
  92.             if (handler != null)
  93.             {
  94.                 handler(this, EventArgs.Empty);
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement