Command

One of the best practices in WPF is to implement the logic behind buttons and menu items as a Command (ICommand) instead of a simple event handler. However, WPF commands require some redundant code, especially to integrate the CanExecute logic.

The Command aspect of the Metalama.Patterns.Wpf open-source package solves this problem by automatically generating the plumbing code.

Metalama.Patterns.Wpf is a production-ready, professionally-supported, and open-source aspect library.

Available for WPF only.

Benefits

  • Boost your productivity. Minimize boilerplate code to implement ICommand properties.
  • Keep your code clean and concise. Your business logic is easier to read and less entangled.
  • Reduce human errors. Eliminate repetitive code that is prone to mistakes.
  • Improve maintainability. Keep your codebase clean and easier to understand.
  • Seamless integration. Works with existing WPF projects without requiring major refactoring.

Features

  • Idiomatically C#: Metalama works with your code, not against it.
  • Supports simple, parameterized, async, and background commands.
  • Integrates with the [Observable] aspect to handle the CanExecute functionality without boilerplate.

Example

In the following example, the [Command] aspect will generate an ExecuteSaveCommand property based on the ExecuteSave method and the CanExecuteSave property.

[Observable]
public partial class MainWindow : Window
{
    public bool HasChanges { get; private set; };

    [Command]
    public void ExecuteSave()
    {
        // Details skipped.
    }

    public bool CanExecuteSave => this.HasChanges;
}

Resources