Skip to content

[Proposal] AsyncCommand #95

@TheCodeTraveler

Description

@TheCodeTraveler

AsyncCommand

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests: Not Started
  • Sample: Not Started
  • Documentation: Not Started

Summary

Enables the Task type to safely be used asynchronously with an ICommand

Detailed Design

AsyncCommand.shared.cs

public class AsyncCommand<TExecute, TCanExecute> : AsyncCommand<TExecute>
{

}

public class AsyncCommand<T> : AsyncCommand
{

}

public class AsyncCommand
{
  public Task ExecuteAsync();

  public event EventHandler CanExecuteChanged;

  public bool IsExecuting { get; }
  public bool AllowsMultipleExecutions { get; }
  public bool CanExecute(TCanExecute? parameter);
  public void RaiseCanExecuteChanged();

  bool ICommand.CanExecute(object parameter);
  void ICommand.Execute(object parameter);
}

## Usage Syntax
[usage]: #usage-syntax

### XAML Usage

N/A

### C# Usage

```cs
class MyViewModel
{
    bool _isBusy;

    public MyViewModel()
    {
        ButtonCommand = new AsyncCommand<int>(buttonClicks => ExecuteButtonCommand(buttonClicks), _ => !IsBusy);
    }

    public IAsyncCommand<int> ButtonCommand { get; }

    public bool IsBusy
    {
        get => _isBusy;
        set
        {
            if(_isBusy != value)
            {
                _isBusy = value;
                ButtonCommand.RaiseCanExecuteChanged();
            }
        }
    }   
    

    async Task ExecuteButtonCommand(int buttonClicks)
    {
        // ...
    }
}

Drawbacks

This duplicates a feature in the MVVM Toolkit.

Alternatives

MVVM Toolkit AsyncRelayCommand

https://docs.microsoft.com/en-us/windows/communitytoolkit/mvvm/asyncrelaycommand

Metadata

Metadata

Labels

championA member of the .NET MAUI Toolkit core team has chosen to champion this featureproposalA fully fleshed out proposal describing a new feature in syntactic and semantic detail

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions