Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I discovered that you need the original way from MSDN if you have multiple controls that invoke the same commands, otherwise each control will new its own RelayCommand. I didn't realize this because my app only has one control per command.</p> <p>So to simplify the code in ViewModels, I'll create a command wrapper class that stores (and lazily instantiates) all the RelayCommands and throw it in my ViewModelBase class. This way users do not have to directly instantiate RelayCommand or DelegateCommand objects and don't need to know anything about them:</p> <pre><code> /// &lt;summary&gt; /// Wrapper for command objects, created for convenience to simplify ViewModel code /// &lt;/summary&gt; /// &lt;author&gt;Ben Schoepke&lt;/author&gt; public class CommandWrapper { private readonly List&lt;DelegateCommand&lt;object&gt;&gt; _commands; // cache all commands as needed /// &lt;summary&gt; /// &lt;/summary&gt; public CommandWrapper() { _commands = new List&lt;DelegateCommand&lt;object&gt;&gt;(); } /// &lt;summary&gt; /// Returns the ICommand object that contains the given delegates /// &lt;/summary&gt; /// &lt;param name="executeMethod"&gt;Defines the method to be called when the command is invoked&lt;/param&gt; /// &lt;param name="canExecuteMethod"&gt;Defines the method that determines whether the command can execute in its current state. /// Pass null if the command should always be executed.&lt;/param&gt; /// &lt;returns&gt;The ICommand object that contains the given delegates&lt;/returns&gt; /// &lt;author&gt;Ben Schoepke&lt;/author&gt; public ICommand GetCommand(Action&lt;object&gt; executeMethod, Predicate&lt;object&gt; canExecuteMethod) { // Search for command in list of commands var command = (_commands.Where( cachedCommand =&gt; cachedCommand.ExecuteMethod.Equals(executeMethod) &amp;&amp; cachedCommand.CanExecuteMethod.Equals(canExecuteMethod))) .FirstOrDefault(); // If command is found, return it if (command != null) { return command; } // If command is not found, add it to the list command = new DelegateCommand&lt;object&gt;(executeMethod, canExecuteMethod); _commands.Add(command); return command; } } </code></pre> <p>This class is also lazily instantiated by the ViewModelBase class, so ViewModels that do not have any commands will avoid the extra allocations.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload