Proposing a new RelayCommand snippet for MVVM Light V4

.NET, Phone, Silverlight, Technical stuff, Windows Phone, Work, WPF
See comments

In MVVM Light V4, I am proposing a new RelayCommand snippet, making it easier to declare and initialize a RelayCommand. I came up with a syntax that allows having the RelayCommand and its initialization in one convenient location.

I am looking for feedback, so leave your comments below!!

RelayCommand

This is the code created by the code snippet after expansion, for a simple RelayCommand.

private RelayCommand _testCommand;

/// <summary>
/// Gets the TestCommand.
/// </summary>
public RelayCommand TestCommand
{
    get
    {
        return _testCommand ?? (_testCommand = new RelayCommand(
            () =>
            {
                // Execute delegate
                throw new NotImplementedException();
            },
            () =>
            {
                // CanExecute delegate
                throw new NotImplementedException();
            }));
    }
}

RelayCommand<T>

This is the code created by the code snippet after expansion, for a generic RelayCommand (with parameter).

private RelayCommand<string> _testAgainCommand;

/// <summary>
/// Gets the TestAgainCommand.
/// </summary>
public RelayCommand<string> TestAgainCommand
{
    get
    {
        return _testAgainCommand ?? (_testAgainCommand = new RelayCommand<string>(
            p =>
            {
                // Execute delegate
                throw new NotImplementedException();
            },
            p =>
            {
                // CanExecute delegate
                throw new NotImplementedException();
            }));
    }
}

Conclusion

Do you find this way of creating the RelayCommands convenient? This limits the number of key press to a minimum (the only parameters to enter are the attribute name, the command’s name and (in the case of the generic RelayCommand) the type of the parameter. Looking for feedback before I consolidate this for V4!

Happy coding!

Laurent

Previous entry | Next blog entry

Comments for Proposing a new RelayCommand snippet for MVVM Light V4