2012-05-12 77 views
2

我正在嘗試使用Anoto-Pen作爲TouchDeviceSurfaceInkCanvas
筆使用打印在一張紙上的座標系獲得其位置,然後將這些位置數據發送給我的應用程序。在那裏,我嘗試通過繼承TouchDevice來將其轉換爲TouchInput,並使用TouchDevice.ReportDown();TouchDevice.ReportMove()等將發送的位置數據和事件轉換爲.NET Touch-Events。移動ScatterViewItems並處理按鈕「單擊」到目前爲止工作。在Microsoft Surface窗口上模擬觸摸輸入

現在的問題是,當我嘗試在InkCanvas上寫字時,只繪製了點。在觀察發生的事件後,似乎InkCanvas未收到OnTouchMove事件。

我在我的SurfaceInkCanvas上爲TouchDownTouchMoveTouchUp註冊了事件處理程序。永不觸發TouchDownTouchMoveTouchUp只有當我開始在SurfaceInkCanvas之外,然後移動到它內部的一個點。

這裏是我的TouchDevice代碼:我有下面的代碼在我App.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Input; 
using System.Text.RegularExpressions; 
using System.Windows; 
using PaperDisplay.PenInput; 
using System.Windows.Media; 
using System.Windows.Threading; 

namespace TouchApp 
{ 
public class PenTouchDevice : TouchDevice 
{ 
    public Point Position { get; set; } 
    public Person Person { get; set; } 

    public PenTouchDevice(Person person) 
     : base(person.GetHashCode()) 
    { 
     Person = person; 
    } 

    public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo) 
    { 
     return new TouchPointCollection(); 
    } 

    public override TouchPoint GetTouchPoint(System.Windows.IInputElement relativeTo) 
    { 
     Point point = Position; 
     if (relativeTo != null) 
     { 
      point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(Position); 
     } 
     return new TouchPoint(this, point, new Rect(point, new Size(2.0, 2.0)), TouchAction.Move); 
    } 

    public void PenDown(PenPointInputArgs args, Dispatcher dispatcher) 
    { 
     dispatcher.BeginInvoke((Action)(() => 
     { 
      SetActiveSource(PresentationSource.FromVisual(Person.Window)); 
      Position = GetPosition(args); 
      if (!IsActive) 
      { 
       Activate(); 
      } 
      ReportDown(); 
     })); 
    } 

    public void PenUp(PenPointInputArgs args, Dispatcher dispatcher) 
    { 
     dispatcher.BeginInvoke((Action)(() => 
     { 
      Position = GetPosition(args); 
      if (IsActive) 
      { 
       ReportUp(); 
       Deactivate(); 
      } 
     })); 

    } 

    public void PenMove(PenPointInputArgs args, Dispatcher dispatcher) 
    { 
     dispatcher.BeginInvoke((Action)(() => 
     { 
      if (IsActive) 
      { 
       Position = GetPosition(args); 
       ReportMove(); 
      } 
     })); 
    } 

    public Point GetPosition(PenPointInputArgs args) 
    { 
     double adaptedX = args.Y - 0.01; 
     double adaptedY = (1 - args.X) - 0.005; 
     return new Point(adaptedX * Person.Window.ActualWidth, adaptedY * Person.Window.ActualHeight); 
    } 
} 
} 

,它被稱爲每一個筆輸入發生時間:

public void HandleEvent(object sender, EventArgs args) 
    { 
     if (typeof(PointInputArgs).IsAssignableFrom(args.GetType())) 
     { 
      PenPointInputArgs pointArgs = (PenPointInputArgs)args; 
      switch (pointArgs.EventType) 
      { 
       case InputEvent.Down: touchDevice1.PenDown(pointArgs, this.Dispatcher); break; 
       case InputEvent.Up: touchDevice1.PenUp(pointArgs, this.Dispatcher); break; 
       case InputEvent.Move: 
       case InputEvent.MoveDown: touchDevice1.PenMove(pointArgs, this.Dispatcher); break; 
      } 

     } 
    } 

預先感謝您。

回答

-1

GetIntermediateTouchPoints不能返回一個空數組,它應包含至少一個點

public override TouchPointCollection GetIntermediateTouchPoints(System.Windows.IInputElement relativeTo) 
{ 
    TouchPointCollection refCollection = new TouchPointCollection(); 
    refCollection.Add(GetTouchPoint(relativeTo)); 
    return refCollection; 
}