Advertisement
Guest User

Untitled

a guest
Jul 5th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Input;
  5.  
  6. namespace MVVMCommsDemo
  7. {
  8.     public class RelayCommand : ICommand
  9.     {
  10.         Action _TargetExecuteMethod;
  11.         Func<bool> _TargetCanExecuteMethod;
  12.  
  13.         public RelayCommand(Action executeMethod)
  14.         {
  15.             _TargetExecuteMethod = executeMethod;
  16.         }
  17.  
  18.         public RelayCommand(Action executeMethod, Func<bool> canExecuteMethod)
  19.         {
  20.             _TargetExecuteMethod = executeMethod;
  21.             _TargetCanExecuteMethod = canExecuteMethod;
  22.         }
  23.  
  24.         public void RaiseCanExecuteChanged()
  25.         {
  26.             CanExecuteChanged(this, EventArgs.Empty);
  27.         }
  28.         #region ICommand Members
  29.  
  30.         bool ICommand.CanExecute(object parameter)
  31.         {
  32.             if (_TargetCanExecuteMethod != null)
  33.             {
  34.                 return _TargetCanExecuteMethod();
  35.             }
  36.             if (_TargetExecuteMethod != null)
  37.             {
  38.                 return true;
  39.             }
  40.             return false;
  41.         }
  42.  
  43.         // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command
  44.         // Prism commands solve this in their implementation
  45.         public event EventHandler CanExecuteChanged = delegate { };
  46.  
  47.         void ICommand.Execute(object parameter)
  48.         {
  49.             if (_TargetExecuteMethod != null)
  50.             {
  51.                 _TargetExecuteMethod();
  52.             }
  53.         }
  54.         #endregion
  55.     }
  56.  
  57.     public class RelayCommand<T> : ICommand
  58.     {
  59.         Action<T> _TargetExecuteMethod;
  60.         Func<T, bool> _TargetCanExecuteMethod;
  61.  
  62.         public RelayCommand(Action<T> executeMethod)
  63.         {
  64.             _TargetExecuteMethod = executeMethod;
  65.         }
  66.  
  67.         public RelayCommand(Action<T> executeMethod, Func<T,bool> canExecuteMethod)
  68.         {
  69.             _TargetExecuteMethod = executeMethod;
  70.             _TargetCanExecuteMethod = canExecuteMethod;
  71.         }
  72.  
  73.         public void RaiseCanExecuteChanged()
  74.         {
  75.              CanExecuteChanged(this, EventArgs.Empty);
  76.         }
  77.         #region ICommand Members
  78.  
  79.         bool ICommand.CanExecute(object parameter)
  80.         {
  81.             if (_TargetCanExecuteMethod != null)
  82.             {
  83.                 T tparm = (T)parameter;
  84.                 return _TargetCanExecuteMethod(tparm);
  85.             }
  86.             if (_TargetExecuteMethod != null)
  87.             {
  88.                 return true;
  89.             }
  90.             return false;
  91.         }
  92.  
  93.         // Beware - should use weak references if command instance lifetime is longer than lifetime of UI objects that get hooked up to command
  94.         // Prism commands solve this in their implementation
  95.         public event EventHandler CanExecuteChanged = delegate { };
  96.  
  97.         void ICommand.Execute(object parameter)
  98.         {
  99.             if (_TargetExecuteMethod != null)
  100.             {
  101.                 _TargetExecuteMethod((T)parameter);
  102.             }
  103.         }
  104.         #endregion
  105.     }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement