Windows 8:如何撤消&使用内置墨水功能重做墨水? [英] Windows 8: How to undo & redo ink using built in Inking functionality?

查看:90
本文介绍了Windows 8:如何撤消&使用内置墨水功能重做墨水?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经基于Microsoft的简化墨迹示例在我的应用程序中实现了墨迹代码:

I've implemented inking code in my app based on the simplified inking sample by microsoft : http://code.msdn.microsoft.com/windowsapps/Input-simplified-ink-sample-11614bbf/view/SourceCode

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

First I made a class that saves data of an operation (draw/delete/clear) like this:

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

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

Then I made one stack for undo ink operations and one for redo operations

//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中删除:

When a user undoes a stroke I push it on the redo stack and delete it from the inkmanager with these methods:

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

然后,当用户单击重做时,将笔划从重做堆栈中弹出并使用以下方法进行绘制:

Then when the user clicks on redo, the stroke is popped off the redo stack and drawn using this method:

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

这一切都起作用,并且笔划显示在网格上. 但是,当我尝试擦除新绘制的笔划时,会出现以下异常:

This all works, and the stroke is displayed back on the grid. However, when I try to erase the newly redrawn stroke I get this exception:

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().但是,即使在尝试重新绘制已删除的墨水(撤消已删除的笔划)时也有问题.

I think the problem lies in the process of adding strokes to the ink manager. I tried making a new stroke and even inheriting from the InkStroke to make it customizable but the InkStroke class is sealed and it doesn't have a constructor. The only was I found to copy it was to do inkStroke.Clone(). But even that has its problems when trying to redraw deleted ink (undo a deleted stroke).

为了避免混淆,我尝试使用最少的代码使这个问题尽可能清楚.

I tried to make this question as clear as possible using the least amount of code to avoid confusion, so let me know if it's insufficient.

在这个问题上,我还将重点放在撤消绘制动作上.取消擦除动作(或什至清除所有"动作)有其自身的一系列问题,因为我无法复制InkStroke对象.

Also in this question I'm focusing on undoing a draw action. Undoing an erasing action (or even "clear all" action) has its own set of problems because I can't make a copy of the InkStroke object.

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

Thanks in advance for your time and consideration.

推荐答案

请参见

See this thread from MSDN, it might be helpful.

这篇关于Windows 8:如何撤消&amp;使用内置墨水功能重做墨水?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆