Codemaru 1.0.0.0
QR 코드, 모바일 랜딩 페이지, vCard 연락처 저장과 명함 디자인을 한 화면에서 제공하는 디지털 명함 서비스입니다.
로딩중...
검색중...
일치하는것 없음
AsyncRelayCommand.cs
이 파일의 문서화 페이지로 가기
1using System.Windows.Input;
2
4
13public sealed class AsyncRelayCommand : ICommand
14{
23 private readonly Func<Task> _execute;
32 private readonly Func<bool>? _canExecute;
41 private bool _isRunning;
42
75 public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
76 {
77 _execute = execute ?? throw new ArgumentNullException(nameof(execute));
78 _canExecute = canExecute;
79 }
80
89 public event EventHandler? CanExecuteChanged;
90
115 public bool CanExecute(object? parameter) => !_isRunning && (_canExecute?.Invoke() ?? true);
116
133 public async void Execute(object? parameter)
134 {
135 if (!CanExecute(parameter))
136 {
137 return;
138 }
139
140 try
141 {
142 _isRunning = true;
144 await _execute();
145 }
146 catch (Exception ex)
147 {
148 System.Diagnostics.Debug.WriteLine($"[AsyncRelayCommand] {ex}");
149 }
150 finally
151 {
152 _isRunning = false;
154 }
155 }
156
166 {
167 System.Windows.Threading.Dispatcher? dispatcher =
168 System.Windows.Application.Current?.Dispatcher;
169
170 if (dispatcher is null || dispatcher.CheckAccess())
171 {
172 CanExecuteChanged?.Invoke(this, EventArgs.Empty);
173 return;
174 }
175
176 dispatcher.InvokeAsync(() => CanExecuteChanged?.Invoke(this, EventArgs.Empty));
177 }
178}
async void Execute(object? parameter)
AsyncRelayCommand(Func< Task > execute, Func< bool >? canExecute=null)