Windows 10 UWP: Undo / Redo on InkCanvas

The InkCanvas in UWP only got pens by default, it can not perform Undo or Redo. To implement this, we will need to code for ourselves. Official document covered Undo functionalilty, but not redo. Today, I have successfully done it, and I'd like to share with you.

First, you need to add two custom buttons on the InkToolbar for Undo / Redo

To implement Redo. The most easy way is to make use of the data structure knowedlege we learned in college. That is, by using a Stack to store the deleted stroke.

Let's review the basics of the Stack fist:

In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peekoperation may give access to the top without modifying the stack.

Because of it's LIFO feature, we can push() the last stroke into the stack when undo the ink, and pop() will return the last undone stroke.