Are Click, Tapped, and PointerPressed synonymous in WinRT-XAML?

后端 未结 4 2059
攒了一身酷
攒了一身酷 2020-11-29 08:09

Does it matter whether I create event handlers for PointerPressed, Click, or Tapped?IOW, is there any functional difference between the following:

相关标签:
4条回答
  • 2020-11-29 08:28

    I know this is an old subject but the winning answer is not correct when it says that Click and Tapped are essentially the same. They are not. If you need some rapid clickin' in a control, Tapped does not respond in the same speed as Click does.

    In very specific scenarios, like in a custom made numeric keypad, if the user wants to input a value like '11' (tapping the same button twice), when you use Tapped, you often miss the second char, so you get a '1', instead a '11'. With Click that doesn't happen.

    ps.: I'm using UWP

    0 讨论(0)
  • 2020-11-29 08:29

    Click is there for backwards compatibility, and is essentially the same as Tapped. Tapped is a "high level gesture" that will translate automatically to a click, tap, pen press, etc. and is what I would recommend to use.

    PointerPressed is not what you want. Here's why: if I press and hold, the PointerPressed event will fire when I initially "press" and then a PointerReleased will fire once it's done. This is more low-level and you can make decisions about how long it was pressed, etc. Typically a long press is NOT what you want to consider a "Click" or a "Tap" because by definition Tap is shorter duration. Therefore, for what you want, "Tap" conveys it best because it translates the gesture for you using the system timing for what is considered a "Tap" vs. a hold and automatically promotes clicks and pen presses to the same event. PointerPressed will fire whenever a button is pressed or a finger is pressed regardless of how long the interaction lasts.

    I have a fairly involved app that demonstrates the various interactions that you can download from http://windows8applications.codeplex.com - just refer to the Chapter 4 sample called "Touch."

    0 讨论(0)
  • 2020-11-29 08:32

    Jeremy's answer isn't entirely accurate. In another thread someone reported having a problem with taps not working the same way as clicks when clicking/tapping in rapid succession and it is easy to reproduce with the code below and could easily be extended to pointer events.

    public sealed partial class MainPage : Page
    {
        private int clicks = 0;
        private int taps = 0;
        public MainPage()
        {
            this.InitializeComponent();
            var button = new Button();
            button.Width = 500;
            button.Height = 200;
            button.Content = string.Format("Clicks: {0}, Taps: {1}", clicks, taps);
            button.Click += (s, e) => button.Content = string.Format("Clicks: {0}, Taps: {1}", ++clicks, taps);
            button.Tapped += (s, e) => button.Content = string.Format("Clicks: {0}, Taps: {1}", clicks, ++taps);
            this.Content = button;
        }
    }
    

    Click is what you should handle on a regular button. It has the logic that is expected of a button. Among the things I can think of are

    • it works with a keyboard as well as with a mouse
    • it works even if you press the button and release it slowly
    • it can be canceled when you press by dragging away from the button and also resumed by dragging back to it after previously pressing the button
    • it is limited to a single button at a time, so if you try to click two buttons together - the first one touched won't click (different than on the original Surface/PixelSense, which supports multi-user interactions!)
    • it likely works better with things like automation and as such with accessibility features of Windows
    • it always works

    As demonstrated in the sample code - the Tapped event doesn't register for all taps if you tap repeatedly. I'm not sure if this is because some underlying gesture recognition logic sees some double taps or simply rejects every other tap for whatever other reason. It works for quick single touch/pen taps or mouse clicks, but is a generic event that you can handle on any UIElement and might be used if you want to distinguish between taps, double taps, right taps (!) or holds on arbitrary UI elements.

    The pointer events are lower level and you can use them to handle slightly more advanced interactions not already built into the framework. As I mentioned - a click consists of a press and accompanying release both occurring on the button, so a similar interaction could be modeled with pointer events. You could, for example, use it to implement some sort of a Whac-A-Mole/Twister combination type of game where you'd want to mash many UI elements at the same time which you couldn't do with clicks.

    0 讨论(0)
  • 2020-11-29 08:53

    You should use Click for Buttons (and ListView, Hyperlink, MenuFlyoutItem, ContentDialog, etc). Tapped is just a pointer event, so it doesn’t get invoked if you use a keyboard, or access key, or automation.

    0 讨论(0)
提交回复
热议问题