我已经通过微软实现着墨代码的基础上简化墨样品我的应用程序:http://code.msdn.microsoft.com/windowsapps/Input-simplified-ink-sample-11614bbf/view/SourceCodeWindows 8:如何使用内置的墨迹功能撤消和重做墨迹?
首先我做了一个类,节省了操作的数据(平局/删除/清除)是这样的:
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();
}
然后,当用户点击重做,笔划被弹出重做堆栈和使用该方法绘制:
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对象。
预先感谢您的时间和考虑。
非常感谢Xyroid。你有没有按照Matt Small在该主题结尾处提出的方式让它工作? – dcdroid 2013-03-25 16:03:05
@Xyroid,你解决了线程中的问题吗?我也正在实现重做/撤销功能。 – Allen4Tech 2013-04-15 10:01:58