删除笔划后难以更新 InkPresenter 视觉对象?

Difficulty updating InkPresenter visual after removing strokes?

我正在创建一个接收手势的 inkcanvas (CustomInkCanvas)。在使用期间的不同时间,我会在墨水画布的不同部分放置额外的面板。一切都很好,未被另一个面板覆盖的 CustomInkCanvas 部分对墨迹和手势做出了适当的响应。

但是,有时无法识别手势,因此在手势处理程序的默认代码中,我试图从 CustomInkCanvas 中移除墨迹——即使它不是最上面的面板。

这是怎么做到的?

注:能想到的我都试过了,包括:

  1. 后台更新为:

    cink.InkPresenter.Dispatcher.Invoke(DispatcherPriority.Background, EmptyDelegate);

  2. 清除笔画:

    Strokes.Clear();

    cink.InkPresenter.Strokes.Clear();

  3. 使视觉无效:

    cink.InkPresenter.InvalidateVisual(); cink.InavlidateVisual();

  4. 甚至

    foreach(笔画中的笔画) { cink.InkPresenter.Strokes.Remove(s); }

这是完整的代码...

 void inkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e)
    {
        CustomInkCanvas cink = sender as CustomInkCanvas;
        ReadOnlyCollection<GestureRecognitionResult> gestureResults = e.GetGestureRecognitionResults();
        StylusPointCollection styluspoints = e.Strokes[0].StylusPoints;

        TextBlock tb;               // instance of the textBlock being used by the InkCanvas.
        Point editpoint;            // user point to use for the start of editing.

        TextPointer at;             // textpointer that corresponds to the lowestpoint of the gesture.
        Run parentrun;              // the selected run containing the lowest point.

        // return if there is no textBlock.
        tb = GetVisualChild<TextBlock>(cink);
        if (tb == null) return;

        // Check the first recognition result for a gesture.
        isWriting = false;
        if (gestureResults[0].RecognitionConfidence == RecognitionConfidence.Strong)
        {
            switch (gestureResults[0].ApplicationGesture)
            {
                #region [Writing]
                default:
                    bool AllowInking;

                    editpoint = GetEditorPoint(styluspoints, EditorPoints.Writing);
                    at = tb.GetPositionFromPoint(editpoint, true);

                    parentrun = tb.InputHitTest(editpoint) as Run;      

                    if (parentrun == null)
                    {
                        AllowInking = true;
                        TextPointer At = tb.ContentEnd;
                        Here = (Run)At.GetAdjacentElement(LogicalDirection.Backward);
                    }
                    else
                    {
                        Here = parentrun;
                        AllowInking = String.IsNullOrWhiteSpace(parentrun.Text);
                    }

*** THIS FAILS TO REMOVE THE INK FROM THE DISPLAY ???? *********
                    if (AllowInking == false)
                    {
                        foreach (Stroke s in Strokes)
                        {
                            cink.InkPresenter.Strokes.Remove(s);
                        }
                        // remove ink from display
                        // Strokes.Clear();
                        // cink.InkPresenter.Strokes.Clear();
                        cink.InkPresenter.InvalidateVisual();
                        cink.InkPresenter.Dispatcher.Invoke(DispatcherPriority.Background, EmptyDelegate);
                        return;
                    }

                    // stop the InkCanvas from recognizing gestures
                    EditingMode = InkCanvasEditingMode.Ink;

                    isWriting = true;

                     break;
                #endregion
            } 
        }
    }

         private static Action EmptyDelegate = delegate() { };

在此先感谢您的帮助。

很高兴得到专家对此的回应,但对于来到这里的其他人来说,显然用于创建手势的笔划尚未添加到 InkCanvas,因此没有任何可删除或清除的内容来自手势处理程序中的 inkcanvas。笔划仅在手势处理程序之后添加到 InkCanvas。这个新手最终得到的解决方案是在不允许使用墨水时设置一个标志,然后在 StrokesChanged 处理程序中对其进行操作,例如:

if (AllowInking == false)
 {
       ClearStrokes = true;
       return;
  }

void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
    {
        if (ClearStrokes == true)
        {
            ClearStrokes = false;
            Strokes.Clear();
            return;
        }

现在一切正常。有没有更好的方法?