本文介绍了InkCanvas 的撤消/重做命令堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 InkCanvas 创建类似绘画的应用程序,我愿意实施撤消和我的应用程序中的重做功能.
I am creating paint like application using InkCanvas , I am willing to implement Undo and Redo functionality in my application .
为 InkCanvas 实现撤消/重做的最佳方法是什么??
Which is the best way to implement Undo / Redo for InkCanvas ??
推荐答案
我知道为时已晚,但如果有人在这里只是为了 InkCanvas 而不是 这个答案可能有帮助:
I know its too late but if someone is here for InkCanvas only than this answer might help:
public partial class MainWindow : Window
{
System.Windows.Ink.StrokeCollection _added;
System.Windows.Ink.StrokeCollection _removed;
private bool handle = true;
public MainWindow()
{
InitializeComponent();
inkCanvas1.Strokes.StrokesChanged += Strokes_StrokesChanged;
}
private void Strokes_StrokesChanged(object sender, System.Windows.Ink.StrokeCollectionChangedEventArgs e)
{
if(handle)
{
_added = e.Added;
_removed = e.Removed;
}
}
private void Undo(object sender, RoutedEventArgs e)
{
handle = false;
inkCanvas1.Strokes.Remove(_added);
inkCanvas1.Strokes.Add(_removed);
handle = true;
}
private void Redo(object sender, RoutedEventArgs e)
{
handle = false;
inkCanvas1.Strokes.Add(_added);
inkCanvas1.Strokes.Remove(_removed);
handle = true;
}
}
在 XAML 中:
<InkCanvas x:Name="inkCanvas1" Width="100" Height="100" Background="Yellow"/>
<Button Content="Undo" Click="Undo" />
<Button Content="Redo" Click="Redo"/>
这篇关于InkCanvas 的撤消/重做命令堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!