如何在UWP中手动向Canvas或UIElement发送触摸事件?

时间:2017-12-20 08:02:20

标签: uwp

我有一个内部有形状的简单画布:

<Canvas x:Name="cnvGameOuter"  Grid.Row="0" >
    <Polygon StrokeThickness="1" x:Name="dashRec"  Height="210" Width="220"  
ManipulationMode="All" Points="80,0,80,130,0,130,0,200,180,200,180,0"  Stroke="#c9c7c7">
    </Polygon>
</Canvas>

我想将触摸事件发送到此Canvas或Shape,就像我们实际触摸它们一样。换句话说,它是一种触摸模拟。

recognizer.GestureSettings = GenerateDefaultSettings();
// Set up pointer event handlers. These receive input events that are used by the gesture recognizer.
element.PointerPressed += OnPointerPressed;
element.PointerMoved += OnPointerMoved;
element.PointerReleased += OnPointerReleased;
element.PointerCanceled += OnPointerCanceled;

此外,我可以更改PointerRoutedEventArgs值,例如位置,设备类型,......?他们是只读属性。我想将手写笔行为转变为手部行为

void OnPointerPressed(object sender, PointerRoutedEventArgs args)
{
    recognizer.ProcessDownEvent(args.GetCurrentPoint(reference));
}

1 个答案:

答案 0 :(得分:0)

这不是一个漂亮的解决方案,但它有效

 public MainPage()
 {
        this.InitializeComponent();
        //asign default click
        cnvGameOuter.PointerPressed += CnvGameOuter_PointerPressed;
        //or in a xaml
        //<Canvas x:Name="cnvGameOuter"  Grid.Row="0" PointerPressed="CnvGameOuter_PointerPressed" >

 }

  private void CnvGameOuter_PointerPressed(object sender, PointerRoutedEventArgs e)
  {
        OGG_PointerPressed(sender, e);
  }

  private void OGG_PointerPressed(object sender, PointerRoutedEventArgs e)
  {
        //your action
  }

把它放在你需要的地方

 OGG_PointerPressed(cnvGameOuter, null);

或者如果你不想打包你可以使用的东西

 CnvGameOuter_PointerPressed(cnvGameOuter, null);

重要

您无法定义PointerRoutedEventArgs并且不应使用的一个问题