■ ICommand 인터페이스에서 매개 변수를 갖는 대리자 명령을 사용하는 방법을 보여준다.
▶ DelegateCommand.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
using System; using System.Collections.Generic; using System.Windows.Input; /// <summary> /// 대리자 명령 /// </summary> /// <typeparam name="TParameter">매개 변수 타입</typeparam> public class DelegateCommand<TParameter> : ICommand { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 변경시 이벤트 - CanExecuteChanged /// <summary> /// 실행 가능 여부 변경시 이벤트 /// </summary> public event EventHandler CanExecuteChanged { add { CommandManager.AddWeakReferenceHandler(ref this.weakReferenceList, value); } remove { CommandManager.RemoveWeakReferenceHandler(this.weakReferenceList, value); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 실행 액션 /// </summary> private readonly Action<TParameter> executeAction; /// <summary> /// 실행 가능 여부 함수 /// </summary> private readonly Func<TParameter, bool> canExecuteFunction; /// <summary> /// 약한 참조 리스트 /// </summary> private List<WeakReference> weakReferenceList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DelegateCommand(executeAction, canExecuteFunction) /// <summary> /// 생성자 /// </summary> /// <param name="executeAction">실행 액션</param> /// <param name="canExecuteFunction">실행 가능 여부 함수</param> public DelegateCommand(Action<TParameter> executeAction, Func<TParameter, bool> canExecuteFunction) { if(executeAction == null) { throw new ArgumentNullException("executeAction"); } this.executeAction = executeAction; this.canExecuteFunction = canExecuteFunction; } #endregion #region 생성자 - DelegateCommand(executeAction) /// <summary> /// 생성자 /// </summary> /// <param name="executeAction">실행 액션</param> public DelegateCommand(Action<TParameter> executeAction) : this(executeAction, null) { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실행 가능 여부 구하기 - CanExecute(parameter) /// <summary> /// 실행 가능 여부 구하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>실행 가능 여부</returns> public bool CanExecute(TParameter parameter) { if(this.canExecuteFunction != null) { return this.canExecuteFunction(parameter); } return true; } #endregion #region 실행하기 - Execute(parameter) /// <summary> /// 실행하기 /// </summary> /// <param name="parameter">매개 변수</param> public void Execute(TParameter parameter) { if(this.executeAction != null) { this.executeAction(parameter); } } #endregion #region 실행 가능 여부 구하기 - ICommand.CanExecute(parameter) /// <summary> /// 실행 가능 여부 구하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>실행 가능 여부</returns> bool ICommand.CanExecute(object parameter) { if(parameter == null && typeof(TParameter).IsValueType) { return this.canExecuteFunction == null; } return CanExecute((TParameter)parameter); } #endregion #region 실행하기 - ICommand.Execute(parameter) /// <summary> /// 실행하기 /// </summary> /// <param name="parameter">매개 변수</param> void ICommand.Execute(object parameter) { Execute((TParameter)parameter); } #endregion #region 실행 가능 여부 변경시 이벤트 발생시키기 - RaiseCanExecuteChangedEvent() /// <summary> /// 실행 가능 여부 변경시 이벤트 발생시키기 /// </summary> public void RaiseCanExecuteChangedEvent() { FireCanExecuteChangedEvent(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 실행 가능 여부 변경시 이벤트 발생시키기 - FireCanExecuteChangedEvent() /// <summary> /// 실행 가능 여부 변경시 이벤트 발생시키기 /// </summary> protected virtual void FireCanExecuteChangedEvent() { CommandManager.CallWeakReferenceHandlers(this.weakReferenceList); } #endregion } |
▶ CommandManager.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
using System; using System.Collections.Generic; /// <summary> /// 명령 관리자 /// </summary> public class CommandManager { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 약한 참조 핸들러 추가하기 - AddWeakReferenceHandler(weakReferenceList, eventHandler) /// <summary> /// 약한 참조 핸들러 추가하기 /// </summary> /// <param name="weakReferenceList">약한 참조 리스트</param> /// <param name="eventHandler">이벤트 핸들러</param> public static void AddWeakReferenceHandler(ref List<WeakReference> weakReferenceList, EventHandler eventHandler) { if(weakReferenceList == null) { weakReferenceList = new List<WeakReference>(); } weakReferenceList.Add(new WeakReference(eventHandler)); } #endregion #region 약한 참조 핸들러 제거하기 - RemoveWeakReferenceHandler(weakReferenceList, eventHandler) /// <summary> /// 약한 참조 핸들러 제거하기 /// </summary> /// <param name="weakReferenceList">약한 참조 리스트</param> /// <param name="eventHandler">이벤트 핸들러</param> public static void RemoveWeakReferenceHandler(List<WeakReference> weakReferenceList, EventHandler eventHandler) { if(weakReferenceList != null) { for(int i = weakReferenceList.Count - 1; i >= 0; i--) { WeakReference weakReference = weakReferenceList[i]; EventHandler existingEventHandler = weakReference.Target as EventHandler; if((existingEventHandler == null) || (existingEventHandler == eventHandler)) { weakReferenceList.RemoveAt(i); } } } } #endregion #region 약한 참조 핸들러 호출하기 - CallWeakReferenceHandlers(weakReferenceList) /// <summary> /// 약한 참조 핸들러 호출하기 /// </summary> /// <param name="weakReferenceList">약한 참조 리스트</param> public static void CallWeakReferenceHandlers(List<WeakReference> weakReferenceList) { if(weakReferenceList != null) { EventHandler[] eventHandlerArray = new EventHandler[weakReferenceList.Count]; int count = 0; for(int i = weakReferenceList.Count - 1; i >= 0; i--) { WeakReference weakReference = weakReferenceList[i]; EventHandler eventHandler = weakReference.Target as EventHandler; if(eventHandler == null) { weakReferenceList.RemoveAt(i); } else { eventHandlerArray[count] = eventHandler; count++; } } for(int i = 0; i < count; i++) { EventHandler eventHandler = eventHandlerArray[i]; eventHandler(null, EventArgs.Empty); } } } #endregion } |