我已经基于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();
}
然后,当用户单击重做时,将笔划从重做堆栈中弹出并使用以下方法绘制:
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对象。
预先感谢您的时间和考虑。
最佳答案
请参阅MSDN中的this线程,这可能会有所帮助。
关于c# - Windows 8:如何使用内置的墨水功能来撤消和重做墨水?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15206693/