superpuccio
superpuccio

Reputation: 12992

Button touchDown and Button touchUp events

I'm developing a Windows Universal App (Windows 8.1 + Winwdows Phone 8.1). I can detect the Click event on a Button doing:

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

I'd like to detect also the "press" event and the "release" event on that button. When the button is pressed I have to change an image in my app. When the button is released the image returns to its init state. I checked the methods related to the Button class, but it seems there's nothing that could help me. Thank you in advance.

Upvotes: 0

Views: 595

Answers (1)

kober
kober

Reputation: 851

You have to implement it, like this

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

Upvotes: 1

Related Questions