如何在UWP中获取按钮按下事件,Windows 10商店应用

时间:2016-08-31 06:26:14

标签: .net xaml windows-store-apps uwp windows-10-universal

我想实现zoomin和zoomout按钮,我想创建按下事件,以便我可以连续调用事件直到按钮保持,但没有指针按下或按下按钮的直接事件。现在我通过下面的代码来做这件事。

 this.btnDockUp.AddHandler(PointerReleasedEvent, new PointerEventHandler(BtnDockUp_PointerReleased), true);
 this.btnDockUp.AddHandler(PointerPressedEvent, new PointerEventHandler(BtnDockUp_PointerPressed), true);

    private void Image_PointerPressed(object sender, PointerRoutedEventArgs args)
    {
        PointerPoint point = args.GetCurrentPoint(sender as UIElement);
        if (args.Pointer.PointerDeviceType == PointerDeviceType.Mouse && point.Properties.IsLeftButtonPressed)
        {
            do
            {
                Debug.WriteLine("Pressed");
            } while (point.Properties.IsCanceled);
            Debug.WriteLine("Released");
        }
    }

    private volatile bool _upPressed = false;
    private void BtnDockUp_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        _upPressed = false;
    }

但它涉及任务和线程,我想要一些更好的方法来实现这一点。

1 个答案:

答案 0 :(得分:0)

仅作记录,

您知道何时按下按钮,也知道何时释放按钮。因此,您知道在这两个时间之间一直按住该按钮。诀窍是实现自己的逻辑,以对应用程序/游戏有用的方式跟踪输入状态。

因此,您可能具有包含以下字段的MouseSnapshot类:

    public Vector2 Position;
    public Vector2 PositionDelta;
    public int WheelDelta;
    public Buttons ButtonPressed;
    public Buttons ButtonHeld;
    public Buttons ButtonReleased;
    public bool PointerExited;
    public bool PointerEntered;
    public bool PointerInRegion;
    // whatever else you need to track

现在,您只需在事件到来时更新MouseSnapshot。