Skip to content

Commit

Permalink
Fixed the issue that parameter wasn't used by the command
Browse files Browse the repository at this point in the history
  • Loading branch information
kirmir committed Oct 29, 2016
1 parent 7ba8e13 commit 739b420
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions WpfAsyncPack/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace WpfAsyncPack
/// </summary>
public class AsyncCommand : PropertyChangeNotifiable, IAsyncCommand
{
private readonly Func<CancellationToken, Task> _command;
private readonly Func<object, CancellationToken, Task> _command;
private readonly Func<object, bool> _canExecute;

private readonly CancelAsyncCommand _cancelCommand = new CancelAsyncCommand();
Expand All @@ -30,20 +30,41 @@ public event EventHandler CanExecuteChanged
/// <summary>
/// Initializes a new instance of the <see cref="AsyncCommand"/> class.
/// </summary>
/// <param name="command">The asynchronous method that supports cancellation.</param>
/// <param name="command">The asynchronous method that accepts parameter and supports cancellation.</param>
/// <param name="canExecute">The method that determines whether the command can be executed in its current state or not.</param>
public AsyncCommand(Func<CancellationToken, Task> command, Func<object, bool> canExecute = null)
public AsyncCommand(Func<object, CancellationToken, Task> command, Func<object, bool> canExecute = null)
{
_command = command;
_canExecute = canExecute;
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncCommand"/> class.
/// </summary>
/// <param name="command">The asynchronous method that supports cancellation.</param>
/// <param name="canExecute">The method that determines whether the command can be executed in its current state or not.</param>
public AsyncCommand(Func<CancellationToken, Task> command, Func<object, bool> canExecute = null)
: this((param, token) => command(token), canExecute)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncCommand"/> class.
/// </summary>
/// <param name="command">The asynchronous method that accepts parameter.</param>
/// <param name="canExecute">The method that determines whether the command can be executed in its current state or not.</param>
public AsyncCommand(Func<object, Task> command, Func<object, bool> canExecute = null)
: this((param, token) => command(param), canExecute)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AsyncCommand"/> class.
/// </summary>
/// <param name="command">The asynchronous method.</param>
/// <param name="canExecute">The method that determines whether the command can be executed in its current state or not.</param>
public AsyncCommand(Func<Task> command, Func<object, bool> canExecute = null) : this(t => command(), canExecute)
public AsyncCommand(Func<Task> command, Func<object, bool> canExecute = null)
: this((param, token) => command(), canExecute)
{
}

Expand Down Expand Up @@ -87,7 +108,7 @@ public async void Execute(object parameter = null)
public async Task ExecuteAsync(object parameter = null)
{
_cancelCommand.NotifyCommandStarting();
Execution = new TaskCompletionNotifier(_command(_cancelCommand.Token));
Execution = new TaskCompletionNotifier(_command(parameter, _cancelCommand.Token));
RaiseCanExecuteChanged();
await Execution.TaskCompletion;
_cancelCommand.NotifyCommandFinished();
Expand Down

0 comments on commit 739b420

Please sign in to comment.