C# Windows 8:如何撤消&;使用内置墨迹功能重做墨迹?
我已经在我的应用程序中基于microsoft提供的简化墨迹示例实现了墨迹代码: 首先,我创建了一个类,用于保存操作(绘制/删除/清除)的数据,如下所示:C# Windows 8:如何撤消&;使用内置墨迹功能重做墨迹?,c#,windows-8,microsoft-metro,windows-runtime,windows-store-apps,C#,Windows 8,Microsoft Metro,Windows Runtime,Windows Store Apps,我已经在我的应用程序中基于microsoft提供的简化墨迹示例实现了墨迹代码: 首先,我创建了一个类,用于保存操作(绘制/删除/清除)的数据,如下所示: public enum eInkOperation { Draw, Delete, None } public class InkOperation { public InkStroke Stroke { get; set; } //requred for drawing from undo publi
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>();
然后,当用户单击“重做”时,笔划将弹出重做堆栈并使用以下方法绘制:
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中的线程,它可能会有所帮助。非常感谢Xyroid。你有没有按照马特·斯莫尔(Matt Small)在该线程末尾建议的方式让它工作?@Xyroid,你解决了线程中的问题了吗?我也在实现重做/撤消功能。
Rect invalidateRect = (Rect)inkManager.ProcessPointerUpdate(e.GetCurrentPoint(_inkingArea));