■ AnimationCompletedTriggerBehavior 엘리먼트를 사용해 애니메이션 종료시 처리하는 방법을 보여준다.
※ 비주얼 스튜디오에서 TestProject(Unpackaged) 모드로 빌드한다.
※ TestProject.csproj 프로젝트 파일에서 WindowsPackageType 태그를 None으로 추가했다.
▶ 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 102 103 |
using System; using System.Collections.Generic; namespace TestProject; /// <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 } |
▶ 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 186 |
using System; using System.Collections.Generic; using System.Windows.Input; namespace TestProject; /// <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 } |
▶ MainPage.xaml
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 |
<?xml version="1.0" encoding="utf-8"?> <Page x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mxic="using:Microsoft.Xaml.Interactions.Core" xmlns:mxi="using:Microsoft.Xaml.Interactivity" xmlns:ctwa="using:CommunityToolkit.WinUI.Animations" xmlns:ctwb="using:CommunityToolkit.WinUI.Behaviors" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" FontFamily="나눔고딕코딩" FontSize="16"> <Button Name="startButton" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="200" Content="애니메이션 시작"> <mxi:Interaction.Behaviors> <mxic:EventTriggerBehavior EventName="Click"> <ctwb:StartAnimationAction Animation="{x:Bind animationSet}" /> </mxic:EventTriggerBehavior> </mxi:Interaction.Behaviors> <ctwa:Explicit.Animations> <ctwa:AnimationSet x:Name="animationSet" IsSequential="True"> <ctwa:ScaleAnimation Duration="00:00:01" From="1" To="1.2"/> <ctwa:ScaleAnimation Duration="00:00:00.1" To="1" /> <mxi:Interaction.Behaviors> <ctwb:AnimationStartedTriggerBehavior> <mxic:InvokeCommandAction Command="{x:Bind ExecuteAnimationStartedCommand}" /> </ctwb:AnimationStartedTriggerBehavior> <ctwb:AnimationCompletedTriggerBehavior> <mxic:InvokeCommandAction Command="{x:Bind ExecuteAnimationCompletedCommand}" /> </ctwb:AnimationCompletedTriggerBehavior> </mxi:Interaction.Behaviors> </ctwa:AnimationSet> </ctwa:Explicit.Animations> </Button> </Page> |
▶ MainPage.xaml.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 |
using Microsoft.UI.Xaml.Controls; namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 애니메이션 시작시 실행 명령 - ExecuteAnimationStartedCommand /// <summary> /// 애니메이션 시작시 실행 명령 /// </summary> public DelegateCommand<object> ExecuteAnimationStartedCommand { get; private set; } #endregion #region 애니메이션 종료시 실행 명령 - ExecuteAnimationCompletedCommand /// <summary> /// 애니메이션 종료시 실행 명령 /// </summary> public DelegateCommand<object> ExecuteAnimationCompletedCommand { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); ExecuteAnimationStartedCommand = new DelegateCommand<object> ( source => { this.startButton.IsEnabled = false; this.startButton.Content = "애니메이션 진행중"; } ); ExecuteAnimationCompletedCommand = new DelegateCommand<object> ( source => { this.startButton.IsEnabled = true; this.startButton.Content = "애니메이션 시작"; } ); } #endregion } |