Button.Click gets called twice

I have my touch handlers set up like this:

    private void Touch_TouchUp(FT5xx6Controller sender, GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
    {
        if (this.touchState)
        {
            this.app.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Up, DateTime.UtcNow);
            this.touchState = false;
        }
    }

    private void Touch_TouchMove(FT5xx6Controller sender, GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
    {
        this.app.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Move, DateTime.UtcNow);
    }

    private void Touch_TouchDown(FT5xx6Controller sender, GHIElectronics.TinyCLR.Drivers.FocalTech.FT5xx6.TouchEventArgs e)
    {
        if (!this.touchState)
        {
            this.app.InputProvider.RaiseTouch(e.X, e.Y, TouchMessages.Down, DateTime.UtcNow);
            this.touchState = true;
        }
    }

But my button .Click handlers get called twice - once with a TouchDownEvent RoutedEvent and once with TouchUpEvent RoutedEvent. Now that would be fine, but this comparison fails:

    private void Network_Click(object sender, RoutedEventArgs e)
    {
        if (e.RoutedEvent == TouchEvents.TouchUpEvent)
        {
            this.Frame.Navigate(typeof(NetworkSettingsPage));
            e.Handled = true;
        }
    }

So, either I need to get Click to stop being called twice (because a touch and release is really just one click) or we need the routed event comparison to the static to result in proper equality. Or, I have another typo somewhere :confused:

This works of course, but is pretty inefficient…

    private void Network_Click(object sender, RoutedEventArgs e)
    {
        if (e.RoutedEvent.Name == "TouchUpEvent")
        {
            this.Frame.Navigate(typeof(NetworkSettingsPage));
            e.Handled = true;
        }
    }

Same bug here: twice click event. I use this:

        private static void Button_Click(object sender, RoutedEventArgs e)
        {
            if (e.RoutedEvent.Name == TouchEvents.TouchUpEvent.Name)
            {
                Debug.WriteLine("Click");
                _led.Write(_led.Read() == GpioPinValue.High ? GpioPinValue.Low : GpioPinValue.High);
            }
        }

Yes, @Bauland, yours is a better solution (using the variable). Magic inline constants like I used are never a good idea. Thanks for pointing out that variable - I will definitely change my code to use it.

The core problem here is that this is a bug (clicks are not touches - clicks should only fire once per gesture). Unfortunately, fixing the bug will break every existing program. That’s a shame, but I think a fix is necessary for consistency with other Windows APIs.

I agree but it has been working from previous preview version (tested with preview2 not preview3).
My solution is better, but I use your code (I’ve just changed the comparison)! Credits are for you.

I’m not disagreeing with you, but this is the thing about previews - you want your breaking changes to occur in the previews rather than getting baked in as long-term weirdness.

So yeah, you can make it work this way, but it would be better if it is consistent with XAML and you could reduce the cognitive load of having to remember this ‘trick’ when in the land of TinyCLR. That will break programs based on the last preview, but will be much better for devs in the long run.

[FWIW, I just submitted a pull request for another breaking change that will bring checkboxes into line with the XAML api - one less thing that is ‘special’ to TinyCLR that we would otherwise need to remember.]