Note that there are some explanatory texts on larger screens.

plurals
  1. POC# CommandManager combined with undo/Redo pattern
    primarykey
    data
    text
    <p>I was currently looking to combine the Undo/Redo (based on <a href="http://www.codeproject.com/Articles/33384/Multilevel-Undo-and-Redo-Implementation-in-C-Part" rel="nofollow">this</a> article) with the Command-Pattern using ICommand. Reading about the idea of CanExecute, and CanExecuteChanged, I found the CommandManager which propagates the changed state to all registered UIElements. However it seems hard to combine these two if the command stores the changed data itself. Here an example of the current situation:</p> <pre><code>interface IUndoCommand : ICommand { void Undo(); } </code></pre> <p>and the Implementation:</p> <pre><code>public class SampleCommand : IUndoCommand { private Point oldPosition; private Shape shape; // Gets the currently moved shape object public MoveCommand(Shape shape, Point oldPosition) { this.oldPosition = oldPosition; this.newPosition = new Point(Cavas.GetLeft(shape),Canvas.GetTop(shape)); this.shape = shape; } public void Execute(object parameter) { Model.PersistChanges(shape, newPosition); } public void Undo() { Model.PersistChanges(shape, oldPosition); } public bool CanExecute(object parameter) { } public event EventHandler CanExecuteChanged; } </code></pre> <h2>Conflict</h2> <p>In this implementation the command stores the changes and hence a new instance needs to be created each time when it's executed. However the CommandManager keeps track of the state and informs the UIElements. If I register each instance of the command there, one UIElement has several, equal commands competing about the CanExecute-state. This seems to break the idea, so how does it work?</p> <p>Of course I could move the state, needed for redo/undo, from the Command to the model, and register exactly one (static) instance of command which will be used allover. But acutally I like the idea of stroring the state in the command.</p> <p>If I'd omit the ICommand Usage the example of the <a href="http://wpftutorial.net/UndoRedo.html" rel="nofollow">wpftutorial.net</a> would work - although I haven't fully got the idea with the action/undo action there.</p> <h2>Question</h2> <p>How do you combine these approaches, the undo/redo + CommandManager? Is the only solution to realize saving the state in the Model (considering MVVM as base) or are there are other opportunities?</p> <p>EDIT: </p> <p>Is it possible to save the state in the command still using the CommandManager? The ICommand-Interface offers the functionality to keep track of the CanExecute-State, which is a nice idea. However, I don't see a possibility to keep this idea while saving the state in the command (and thus need several different instances).</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.
 

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