按钮touchDown和Button touchUp事件

时间:2015-09-21 09:52:29

标签: wpf xaml windows-phone-8.1 windows-8.1 win-universal-app

我正在开发Windows Universal App(Windows 8.1 + Winwdows Phone 8.1)。我可以在Click上检测到Button事件:

<Button x:Name="menuButton" Click="menuButton_Click" />

我也想检测&#34;按&#34;事件和&#34;发布&#34;该按钮上的事件。按下按钮后,我必须在我的应用程序中更改图像。释放按钮后,图像将返回其初始状态。我检查了与Button类相关的方法,但似乎没有什么可以帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你必须像这样实现它

public class MyButton : Button
{
    public event RoutedEventHandler MyPointerPressed;
    public event RoutedEventHandler MyPointerReleased;
    public event RoutedEventHandler MyPointerExited;
    public event RoutedEventHandler MyPointerMoved;

    public event EventHandler HoldingStarted;

    protected override void OnPointerPressed(PointerRoutedEventArgs e)
    {
        base.OnPointerPressed(e);

        MyPointerPressed?.Invoke(this, e);
    }

    protected override void OnPointerMoved(PointerRoutedEventArgs e)
    {
        base.OnPointerMoved(e);

        MyPointerMoved?.Invoke(this, e);
    }

    protected override void OnPointerReleased(PointerRoutedEventArgs e)
    {
        base.OnPointerReleased(e);

        MyPointerReleased?.Invoke(this, e);
    }

    protected override void OnPointerExited(PointerRoutedEventArgs e)
    {
        base.OnPointerExited(e);

        MyPointerExited?.Invoke(this, e);
    }

    protected override void OnHolding(HoldingRoutedEventArgs e)
    {
        if (e.HoldingState == HoldingState.Started)
        {
            HoldingStarted?.Invoke(this, new EventArgs());
            return;
        }

        base.OnHolding(e);
    }
}