User commands in wpf

I am working on a WPF application that has a toolbar / menu that will be used for several user commands. Probably around 15-20. I saw the documentation on how to create custom commands, but none of them necessarily apply to what I'm trying to do.

I use a controller to process business logic in my application, and I try to ensure that my mind does not do any logic at all.

I would like to create a directory in my project that contains custom command classes so that I can separate them from the controller and the view, but I would still like them to be called from a view, such as the normal commmand.

I also saw the use of the DelegateCommand class, but am not entirely sure that this is the direction I want to go.

I would like to have an arbitrary custom command class like the following

public CustomCommand: ICommandd { public bool CanExecute(object parameter) { //arbitrary logic } public void Execute(object parameter) { } } 

The idea is that I would have 10-20 of them, and I want them to be separated from everything else, and if necessary they would be called.

I know there is a way to separate my user commands, but I'm not sure.

I am new to using commands, so I'm still trying to understand the concept.

thanks,

+4
source share
3 answers

I ended up answering my question through the following post, http://www.codeproject.com/KB/WPF/CentralizingWPFCommands.aspx?display=Print

0
source

The concept is that you bind a command to a button, and the command controls two properties of this button: "on click" and "enabled", as a result of which you laid out the interface.

The main reason you want to execute a command is to bind buttons to actions in your view model.

If you create one custom command that takes action as a constructor parameter, you can connect methods from your view model directly to your team.

 public class RelayCommand: ICommandd { Action action; Func<bool> canExecute; public RelayCommand(Action action) : this(action, () => true) {} public RelayCommand(Action action, Func<bool> canExecute) { this.action = action; this.canExecute = canExecute; } public bool CanExecute(object parameter) { return canExecute(); } public void Execute(object parameter) { action(); } } 

Use in your view model would be

 public RelayCommand SaveCommand { get; set; } SaveCommand = new RelayCommand(OnSave); public void Save() { // save logic... } 

If you want to connect CanExecute, you can also use the second ctor and provide the CanSave method.

 public RelayCommand SaveCommand { get; set; } SaveCommand = new RelayCommand(OnSave, CanSave); public void Save() { // save logic... } public bool CanSave() { return // ... } 

As you may have noticed, I reset the command parameter in my implementation. This will be sufficient in most cases and will save additional parameters in the handler methods. For the 10% remaining, I implemented RelayCommand<T> , which takes an action instead of Action and changes the Execute method to

  public void Execute(object parameter) { action((T)parameter); } 

which requires a parameterized handler

 SaveCommand = new RelayCommand<SomeType>(OnSave); public void Save(SomeType toSave) { // save logic using parameter } 

This saves you from all the casting problems you encounter when using object variables, and keeps your view type models safe.

+4
source

Use RelayCommand , you do not need to create a class for each command, you just add both methods to the constructor, like lambda expressions / delegates.

I use all this around my projects, this is a real time saver.

+1
source

Source: https://habr.com/ru/post/1344378/


All Articles