I'm implementing an undo/redo feature to an InkCanvas
, so I need to save the content of the InkCanvas
in a list (not a file) every time it gets modified. I'm currently saving it in an InMemoryRandomAccessStream
but since it needs to be an instance I can't use it in a list. Is there a way I could store more than one content and retrieve when necessary?
According to your requirement of undo/redo feature, you can use AddStroke
and DeleteSelected
method to add or delete the stroke that you want to undo or redo. Here is my code, you can have a reference.
private List<InkStroke> undoList = new List<InkStroke>();
private void Undo(object sender, RoutedEventArgs e)
{
IReadOnlyList<InkStroke> inkList = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
if (inkList.Count > 0)
{
InkStroke undoStroke = inkList[inkList.Count - 1];
undoStroke.Selected = true;
undoList.Add(undoStroke.Clone());
inkCanvas.InkPresenter.StrokeContainer.DeleteSelected();
}
}
private void Redo(object sender, RoutedEventArgs e)
{
if (undoList.Count > 0)
{
InkStroke redoStroke = undoList[undoList.Count - 1];
inkCanvas.InkPresenter.StrokeContainer.AddStroke(redoStroke);
undoList.Remove(redoStroke);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.