我正在创建一个接收手势的inkcanvas(CustomInkCanvas)。在使用过程中的不同时间,我在墨水扫描的不同部分放置了额外的面板。一切都很好,而另一个面板未涵盖的CustomInkCanvas部分会对墨水和手势做出适当的响应。
然而,偶尔会识别出手势,所以在手势处理程序的默认代码中,我试图从CustomInkCanvas中删除墨水 - 即使它不是最上面的面板。
这是怎么做到的?
注意:我已经尝试了我能想到的一切,包括:
调度程序,后台更新为:
cink.InkPresenter.Dispatcher.Invoke(DispatcherPriority.Background,EmptyDelegate);
使用以下内容清除笔画:
Strokes.Clear();
cink.InkPresenter.Strokes.Clear();
使用以下内容使视觉效果失效:
cink.InkPresenter.InvalidateVisual(); cink.InavlidateVisual();
甚至
foreach(笔画中的中风) { cink.InkPresenter.Strokes.Remove(一个或多个); }
这是完整的代码......
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() { };
提前感谢您的帮助。
答案 0 :(得分:0)
让大师对此做出反应会很好,但是对于其他人来到这里,显然创建手势的笔画还没有被添加到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;
}
现在一切正常。还有更好的方法吗?