Xamarin Forms Async Commands with Parameters

A quick mini post to show how to pass parameters to asynchronous commands in Xamarin Forms. I keep forgetting the syntax for this so it’s useful to have a reminder!

public class DemoViewModel
{
    public DemoViewModel()
    {
        this.DemoCommand = new Command<double>(async (x) => await CalculateSquareSlowly(x));
    }

    public ICommand DemoCommand { get; private set; }

    private async Task<double> CalculateSquareSlowly(double number)
    {
        await Task.Delay(2000);
        return number * number;
    }
}
Advertisements
Xamarin Forms Async Commands with Parameters

2 thoughts on “Xamarin Forms Async Commands with Parameters

    1. martynnw says:

      x is the value that will be passed to the command – so in the example above it’s the value that will be used in CalculateSquareSlowly

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s