c# - Windows 8 : How to undo & redo ink using built in Inking functionality?

标签 c# windows-8 microsoft-metro windows-runtime windows-store-apps

我已经根据 Microsoft 的简化墨迹书写示例在我的应用程序中实现了墨迹书写代码:http://code.msdn.microsoft.com/windowsapps/Input-simplified-ink-sample-11614bbf/view/SourceCode

首先我创建了一个类来保存操作(绘制/删除/清除)的数据,如下所示:

public enum eInkOperation
{
    Draw,
    Delete,
    None
}

public class InkOperation
{
    public InkStroke Stroke { get; set; } //requred for drawing from undo
    public eInkOperation Operation { get; set; }

    public InkOperation(InkStroke stroke, eInkOperation inkOperation)
    {
        Stroke = stroke.Clone(); //needs to be cloned for AddStroke to work
        Operation = inkOperation;
    }
}

然后我做了一个堆栈用于撤消墨水操作,一个堆栈用于重做操作

//stack of normal operations
Stack<InkOperation> _undoStack = new Stack<InkOperation>(); 
//Undo action will pop them off of the undo stack and push them onto the redo stack
Stack<InkOperation> _redoStack = new Stack<InkOperation>();

当用户撤消笔划时,我将其压入重做堆栈并使用以下方法将其从 inkmanager 中删除:

private void RedoStackPush(InkOperation inkOperation)
{
    inkOperation.Stroke = inkOperation.Stroke.Clone();
    _redoStack.Push(inkOperation);
}

  private void DeleteStroke(InkStroke stroke)
    {                            
        stroke = inkManager.GetStrokes().Last();
        stroke.Selected = true;
        inkManager.DeleteSelected();
    }

然后当用户点击 redo 时,笔划会从 redo 堆栈中弹出并使用此方法绘制:

private void DrawStroke(InkStroke stroke)
{
        if (stroke!=null)
        {
            inkManager.Mode = InkManipulationMode.Inking;
            inkManager.AddStroke(stroke);
        }
        renderer.Clear(); //this renderer object smooths the strokes
        //and adds them as Path objects to the desired control (Grid, etc)
        renderer.AddInk(inkManager.GetStrokes());
}

一切正常,笔画显示在网格上。 但是,当我尝试删除新重绘的笔划时,我得到了这个异常:

AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

这发生在:

public void PointerMoved(PointerRoutedEventArgs e)
{
    try
    {
        var pointerPoint = e.GetCurrentPoint(_inkingArea);
        var pointerEventType = InkHelpers.GetPointerEventType(e);
        if (pointerId == (int)pointerPoint.PointerId)
        {
            switch (inkManager.Mode)
            {
                case InkManipulationMode.Inking:
                case InkManipulationMode.Selecting:
                    //process intermediate points
                    var intermediatePoints = e.GetIntermediatePoints(_inkingArea);
                    for (int i = intermediatePoints.Count - 1; i >= 0; i--)
                    {
                        inkManager.ProcessPointerUpdate(intermediatePoints[i]);
                    }
                    //live rendering
                    renderer.UpdateLiveRender(pointerPoint);
                    break;
                case InkManipulationMode.Erasing:
                    //check if something has been erased
                    //in erase mode InkManager.ProcessPointerUpdate returns an invalidate rectangle:
                    //if it is not degenerate, something has been erased
                    //in erase mode don't bother processing intermediate points

                    //If inkManager.ProcessPointerUpdate throws an exception, it crashes the app regardless of any catches
                    Rect invalidateRect = (Rect)inkManager.ProcessPointerUpdate(e.GetCurrentPoint(_inkingArea));
                    if (invalidateRect.Height != 0 && invalidateRect.Width != 0)
                    {
                        //we don't know what has been erased so we clear the render
                        //and add back all the ink saved in the ink manager
                        renderer.Clear();

                        var remainingStrokes = inkManager.GetStrokes();
                        renderer.AddInk(remainingStrokes);
                    }
                    break;
                default:
                    break;
            }
        }
    }
    catch (Exception) { }
}

在这一行:

Rect invalidateRect = (Rect)inkManager.ProcessPointerUpdate(e.GetCurrentPoint(_inkingArea));

我认为问题出在给墨水管理器添加笔划的过程中。我尝试创建一个新的笔划,甚至从 InkStroke 继承以使其可自定义,但 InkStroke 类是密封的并且它没有构造函数。我发现唯一要复制的是执行 inkStroke.Clone()。但即便如此,在尝试重绘已删除的墨迹(撤消已删除的笔划)时也存在问题。

我试图使用最少的代码使这个问题尽可能清楚,以避免混淆,所以如果不够,请告诉我。

同样在这个问题中,我专注于撤消绘制操作。撤消删除操作(甚至“全部清除”操作)有其自身的一系列问题,因为我无法复制 InkStroke 对象。

提前感谢您的时间和考虑。

最佳答案

参见 this来自 MSDN 的线程,它可能会有所帮助。

关于c# - Windows 8 : How to undo & redo ink using built in Inking functionality?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15206693/

相关文章:

c# - 如何从动态生成的程序集中引用 GAC 程序集?

c# - XAML 中的 ItemsPanelTemplate 忽略 [ContentProperty] 属性

windows-8 - 是否有 Windows 8 应用程序目录网站(不是可从 Windows 8 中访问的网站)?

windows-8 - XAML 在本地命名空间中找不到引用

graphics - 将 SVG 转换为 XAML

c# - Godaddy .net 网站托管问题 - 没有此类主机已知错误

c# - C# 中区 block 链的 ECDSA secp256k1 离线签名者

javascript - 重定向到 ASP MVC 中的另一个页面后,如何使 Toastr Notification 保持事件状态

wpf - 为堆栈面板内的控件赋予相等的宽度

windows-8 - 强制在 metro 中显示键盘?