How to use a parameter in a Task-Based DelegateCommand in Prism

139 Views Asked by At

Can I use a parameter in a task-based DelegateCommand (Prism.Commands):

(https://prismlibrary.com/docs/commanding.html)

public class ArticleViewModel
{
    public DelegateCommand SubmitCommand { get; private set; }

    public ArticleViewModel()
    {
        SubmitCommand = new DelegateCommand<object>(async ()=> await Submit());
    }

    Task Submit(object parameter)
    {
        return SomeAsyncMethod(parameter);
    }
}
1

There are 1 best solutions below

0
Haukinger On

Can I use a parameter in a task-based DelegateCommand?

Sure.

internal class ArticleViewModel : BindableBase
{
    public ArticleViewModel()
    {
        SubmitCommandWithMethodGroup = new DelegateCommand<object>( SomeAsyncMethod );
        SubmitCommandWithLambda = new DelegateCommand<object>( async x => { var y = await Something(x); await SomethingElse(y); } );
    }

    public DelegateCommand<object> SubmitCommandWithMethodGroup { get; }
    public DelegateCommand<object> SubmitCommandWithLambda { get; }
}