我试图将Anoto-Pen与TouchDevice用作SurfaceInkCanvas
笔使用打印在纸上的坐标系得出其位置,然后将该位置数据发送到我的应用程序。在这里,我尝试通过子类化TouchInput将其转换为TouchDevice,并使用TouchDevice.ReportDown();TouchDevice.ReportMove()等将发送位置数据和事件转换为.NET Touch-Events。四处移动ScatterViewItems并处理按钮“单击到目前为止。

现在的问题是,当我尝试在InkCanvas上书写时,只会绘制点。观察到触发的事件后,似乎InkCanvas没有收到OnTouchMove事件。

我在TouchDown上注册了TouchMoveTouchUpSurfaceInkCanvas的事件处理程序。 TouchDown永远不会触发。 TouchMoveTouchUp仅当我在SurfaceInkCanvas之外开始,然后移动到其中的某个点时。

这是我的TouchDevice的代码:

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);
    }
}
}


我的App.xaml.cs中有以下代码,每次输入笔时都会调用它:

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;
            }

        }
    }


先感谢您。

最佳答案

GetIntermediateTouchPoints不得返回空数组,它应至少包含一个点

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

关于c# - 在Microsoft Surface Window上模拟触摸输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10564914/

10-09 17:31