2

我已經通過微軟實現着墨代碼的基礎上簡化墨樣品我的應用程序: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對象。

預先感謝您的時間和考慮。

回答

1

請參閱this來自MSDN的線程,這可能會有所幫助。

+0

非常感謝Xyroid。你有沒有按照Matt Small在該主題結尾處提出的方式讓它工作? – dcdroid 2013-03-25 16:03:05

+1

@Xyroid,你解決了線程中的問題嗎?我也正在實現重做/撤銷功能。 – Allen4Tech 2013-04-15 10:01:58