【问题标题】:Custom TouchDevice triggers Touch events at incorrect position自定义 TouchDevice 在不正确的位置触发 Touch 事件
【发布时间】:2015-05-28 11:08:22
【问题描述】:

我正在使用自定义 TouchDevice(下面的代码)与 UIElements 交互。此 TouchDevice 导致 TouchEnter 和 TouchLeave 事件在我传递给Move(x,y) 的屏幕坐标右下角的 UIElements 上触发,而不是在这些坐标处的 UIElement 上触发。我该如何解决这个问题?

此问题出现在我的 Surface Pro 3 平板电脑(触摸屏,Windows 8.1)上,但不在我的桌面(无触摸屏,Windows 7)上。

我首先假设原因是 GetTouchPoint(IInputElement relativeTo) 的错误实现。但是,此方法仅在将 relativeTo 设置为 null 时调用。

public class CustomTouchDevice : TouchDevice
    {
        public Point Position { get; set; }

        private GazeTouchDevice(int id) : base(id)
        {
            SetActiveSource(PresentationSource.FromVisual(Application.Current.MainWindow));
        }

        public void Move(int x, int y)
        {
            if(!IsActive)
            {
                Activate();
                ReportDown();
            }
            Position = new Point(x, y);
            ReportMove();
        }

        public void Lost()
        {
            if(IsActive)
            {
                ReportUp();
                Deactivate();
            }
        }

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

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

提前致谢!

【问题讨论】:

    标签: c# wpf touch


    【解决方案1】:

    替换GetTouchPoint 的第一行解决了问题:

    Point point = ActiveSource.RootVisual.PointFromScreen(Position);
    

    显然,我的应用程序使用的坐标系与平板电脑上的屏幕坐标系不同。我怀疑 WPF 显然选择不在超高清平板电脑屏幕上使用全分辨率(2160x1440 像素/216 ppi)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多