Create UndoRedoManager V1_0_0 For WPF C#
In post, I will talk about undo and redo manager (UndoRedoManager.cs) for C# (WPF and WindowsForm both). It's works properly in WPF but, I can't take responsibility for WindowsForm. Because this functionality tested in WPF only. So, This is version V1_0_0 and the below is code of UndoRedoManager.cs.
using System; using System.Collections.Generic; using System.IO; using System.Windows.Markup; using System.Xml; namespace Saatody { public class UndoRedoManager { public MainWindow Window { set; get; } private int Pointer = 0; private List<string> States = new List<string>(); public void RecordState(object State) { try { States.RemoveRange(Pointer + 1, States.Count - (Pointer + 1)); } catch(Exception Ex) { } States.Add(XamlWriter.Save(State)); Pointer = States.Count - 1; Window.Console.Text = "Record State (" + Pointer + "): " + XamlWriter.Save(State) + "\n" + Window.Console.Text; } public object UndoState() { if (Pointer > 0) { Pointer -= 1; } Window.Console.Text = "Undo State (" + Pointer + "): " + States[Pointer] + "\n" + Window.Console.Text; StringReader StringReader = new StringReader(States[Pointer]); XmlReader XmlReader = XmlReader.Create(StringReader); object State = (object)XamlReader.Load(XmlReader); StringReader.Close(); XmlReader.Close(); return State; } public object RedoState() { if (Pointer < (States.Count - 1)) { Pointer += 1; } Window.Console.Text = "Redo State (" + Pointer + "): " + States[Pointer] + "\n" + Window.Console.Text; StringReader StringReader = new StringReader(States[Pointer]); XmlReader XmlReader = XmlReader.Create(StringReader); object State = (object)XamlReader.Load(XmlReader); StringReader.Close(); XmlReader.Close(); return State; } } }
- RecordState()
- UndoState()
- RedoState()
RecordState() - Record An Object As State
Syntax of RecordState()
public void RecordState(object State)
UndoState() - Undo State From List
In UndoState(), User will get an object according to states position. Now user need to manually casting and update that object. The function UndoState(), You can use in the Keyup event where Ctrl + Z shortcut you applied or you can use on any button or menu click event.
Syntax of UndoState()
public object UndoState()
RedoState() - Redo State From List
Syntax of RedoState()
public object RedoState()
In Second post, I will give you an example that why you can understand how you can use this API in your application.
Checkout my second post on UndoRedoManager.cs example: https://me-saatody.blogspot.com/2020/09/example-of-undoredomanager-v100-wpf-c.html
Comments
Post a Comment