undo/redo command stack for InkCanvas

走远了吗. 提交于 2021-02-07 04:00:05

问题


I am creating paint like application using InkCanvas , I am willing to implement Undo and Redo functionality in my application .

Which is the best way to implement Undo / Redo for InkCanvas ??


回答1:


I've implemented undo / redo for a WPF application and ended up publishing my undo / redo code to http://muf.codeplex.com/. You can also get it via NuGet. Just look for "MUF" or "Monitored Undo Framework". It includes support for Silverlight 4.0, as well as .NET 3.5, 4.0, and WP7.

In my WPF app, we also had an InkCanvas that supported Undo / Redo. In my case, the strokes for the InkCanvas were saved to a database with the rest of the data. I hooked the various events on InkCanvas to detect when the strokes had changed. Then used these events to update the entities.

The entities tracked the changes to the strokes and integrated into the Undo / Redo library. When the user clicked Undo, the library would alter the entities back to their original state. Then I'd push those strokes back into the InkCanvas and trigger a layout update.

Comments and questions are welcome on the codeplex site ( http://muf.codeplex.com/ ). You'll also find complete documentation and sample apps there.




回答2:


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;
    }
}

And in XAML:

<InkCanvas x:Name="inkCanvas1" Width="100" Height="100" Background="Yellow"/>
<Button Content="Undo" Click="Undo" />
<Button Content="Redo" Click="Redo"/>


来源:https://stackoverflow.com/questions/6368517/undo-redo-command-stack-for-inkcanvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!