Prism.Commands.DelegateCommand

Just a quick one on how to use the DelegateCommand that comes with PRSIM libraries. The name space for this command is  Prism.Commands. The following usage shows how to use the command with and without command parameter.

Most of the time we use only the Command so we can declare it as following in our viewmodel which will be bound to view:

public ICommand NormalCommand
{
get
{
return new DelegateCommand(() =>
{
MessageBox.Show(“Hello World without command parameter”);
});
}
}

 

when we need the command parameter we can use the same syntax but with a generic parameter for lambda expression:

public ICommand ParameterisedCommand
{
get
{
return new DelegateCommand<string>((s) =>
{
MessageBox.Show(“Hello world with parameter :  ” + s);
});
}
}

In the above usage, we have used a string as the generic parameter because we expect the parameter of the type string, however if we expect the parameter of any complex type then we can use object in the generic parameter and then typecast it to the required type.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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